Skip to content

Commit 41417e6

Browse files
committed
Bonus - Iteration ironhack-labs#8.1: Product of diagonals
1 parent c1e5d16 commit 41417e6

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/functions-and-arrays.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,38 @@ const greatestProduct = (matrix) => {
273273
}
274274
}
275275

276-
console.log(greatestProduct(matr))
276+
console.log(greatestProduct(matr))
277+
278+
279+
const greatestProductOfDiagonals = matrix =>{
280+
let max = 0;
281+
//1. iterate the rows;
282+
for (i=0; i<matrix.length; i++){
283+
284+
//2. iterate the columns;
285+
for (j=0; j<matrix.length; j++){
286+
287+
// check the maximum product in diagonal going through down - right
288+
if (((i - 3) >= 0) && ((j - 3) >= 0)){
289+
result = (matrix[i][j] * matrix[i - 1][j - 1] *
290+
matrix[i - 2][j - 2] * matrix[i - 3][j - 3])
291+
292+
if (max < result){
293+
max = result }
294+
}
295+
296+
//check the maximum product in diagonal going through up - right
297+
if (((i - 3) >= 0 ) && ((j - 1) <= 0)){
298+
result = (matrix[i][j] * matrix[i - 1][j + 1] *
299+
matrix[i - 2][j + 2] * matrix[i - 3][j + 3])
300+
301+
if (max < result){
302+
max = result }
303+
}
304+
305+
306+
}return max
307+
}
308+
}
309+
310+
greatestProductOfDiagonals (matr)

0 commit comments

Comments
 (0)