Skip to content

Commit e4a3a3d

Browse files
committed
// Bonus - Iteration ironhack-labs#8.1: Product of diagonals
1 parent 295c32b commit e4a3a3d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/functions-and-arrays.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,40 @@ function greatestProduct(matrix) {
232232
console.log(currentHighestProduct)
233233
return currentHighestProduct
234234
}
235+
236+
// Bonus - Iteration #8.1: Product of diagonals
237+
238+
function greatestProductOfDiagonals(matrix) {
239+
let currentHighestProduct = 0
240+
let currentGreatestProductArr = []
241+
let index = []
242+
for (let i = 0; i < matrix.length; i++) {
243+
for (let j = 0; j < matrix[i].length; j++) {
244+
let tempProductDownRight = 0
245+
let tempProductDownLeft = 0
246+
if(j+3< matrix[i].length && i+3< matrix.length) {
247+
tempProductDownRight = matrix[i][j] * matrix[i+ 1][j + 1] * matrix[i+ 2][j + 2] * matrix[i+ 3][j + 3]
248+
}
249+
if(j-3 > 0 && i+3< matrix.length) {
250+
tempProductDownLeft = matrix[i][j] * matrix[i + 1][j - 1] * matrix[i+ 2][j - 2] * matrix[i+ 3][j - 3]
251+
}
252+
if(tempProductDownRight > tempProductDownLeft) {
253+
if(currentHighestProduct < tempProductDownRight) {
254+
currentHighestProduct = tempProductDownRight
255+
currentGreatestProductArr = [matrix[i][j] , matrix[i+ 1][j + 1] , matrix[i+ 2][j + 2] , matrix[i+ 3][j + 3]]
256+
index = [i,j]
257+
}
258+
}
259+
else {
260+
if(currentHighestProduct < tempProductDownLeft) {
261+
currentHighestProduct = tempProductDownLeft
262+
currentGreatestProductArr = [matrix[i][j] , matrix[i + 1][j - 1] , matrix[i+ 2][j - 2] , matrix[i+ 3][j - 3]]
263+
index = [i,j]
264+
}
265+
}
266+
}
267+
}
268+
console.log(currentHighestProduct)
269+
console.log(currentGreatestProductArr + " index at x=" + index[0] + " y="+ index[1])
270+
return currentHighestProduct
271+
}

0 commit comments

Comments
 (0)