Skip to content

Commit 64ff2d0

Browse files
fixed iteration ironhack-labs#8.1
1 parent b4fe63a commit 64ff2d0

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed

src/functions-and-arrays.js

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,7 @@ function howManyTimes(words, target) {
177177
// Iteration #8: Bonus
178178
const matrix = [
179179
[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
180-
[
181-
49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62,
182-
0,
183-
],
180+
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62,0],
184181
[
185182
81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36,
186183
65,
@@ -241,28 +238,62 @@ const matrix = [
241238
];
242239

243240
function greatestProduct(matrix) {
244-
let oneCounter = 0;
245-
let twoCounter = 0;
241+
let prodMax = 0;
242+
243+
let currRow = 0;
244+
let currCol = 0;
245+
246+
for (let i = 0; i < matrix.length; i++) {
247+
currRow = matrix[i].slice(0,4).reduce((prev, curr) => prev*curr, 1)
248+
currCol = matrix.slice(0,4).map(row => row[i]).reduce((prev, curr) => prev*curr, 1)
249+
250+
if (currRow > prodMax) prodMax = currRow;
251+
if (currCol > prodMax) prodMax = currCol;
252+
253+
for (let j = 4; j < matrix[i].length; j++) {
254+
currRow *= matrix[i][j]
255+
currRow /= matrix[i][j-4]
256+
257+
currCol *= matrix[j][i]
258+
currCol /= matrix[j-4][i]
259+
260+
if (currRow > prodMax) prodMax = currRow;
261+
if (currCol > prodMax) prodMax = currCol;
262+
}
263+
}
264+
265+
return prodMax;
266+
}
267+
268+
function greatestProductOfDiagonals(matrix) {
269+
let prodMax = 0;
270+
271+
let currFor = 0;
272+
let currBack = 0;
246273

247274
for (let i = 0; i < matrix.length; i++) {
248-
for (let j = 0; j < matrix[i].length; j++) {
249-
if (matrix[i][j] !== 1 && matrix[i][j] !== 2) {
250-
return 0;
251-
}
252-
253-
if (matrix[i][j] === 1 && twoCounter === 0) {
254-
oneCounter++;
255-
} else if (matrix[i][j] === 2 && oneCounter === 0) {
256-
twoCounter++;
257-
} else {
258-
return 0;
259-
}
275+
currRow = matrix[i].slice(0,4).reduce((prev, curr) => prev*curr, 1)
276+
currCol = matrix.slice(0,4).map(row => row[i]).reduce((prev, curr) => prev*curr, 1)
277+
278+
if (currRow > prodMax) prodMax = currRow;
279+
if (currCol > prodMax) prodMax = currCol;
280+
281+
for (let j = 4; j < matrix[i].length; j++) {
282+
currRow *= matrix[i][j]
283+
currRow /= matrix[i][j-4]
284+
285+
currCol *= matrix[j][i]
286+
currCol /= matrix[j-4][i]
287+
288+
if (currRow > prodMax) prodMax = currRow;
289+
if (currCol > prodMax) prodMax = currCol;
260290
}
261291
}
262292

263-
return oneCounter > 0 ? 1 : 16;
293+
return prodMax;
264294
}
265295

296+
266297
// The following is required to make unit tests work.
267298
/* Environment setup. Do not modify the below code. */
268299
if (typeof module !== "undefined") {

0 commit comments

Comments
 (0)