Skip to content

Commit 371553a

Browse files
committed
Bonus - Iteration ironhack-labs#8.1: Product of diagonals with a different approach
1 parent dc99953 commit 371553a

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/functions-and-arrays.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,6 @@ function transposeMatrix(matrixToTranspose) {
229229
return transposedMatrix
230230
}
231231

232-
const tm = [
233-
[1, 2, 3, 4, 5],
234-
[1, 20, 3, 4, 5],
235-
[1, 20, 3, 4, 5],
236-
[1, 20, 3, 4, 5],
237-
[1, 4, 3, 4, 5]
238-
]
239-
240232
function greatestProduct(matrixToTest) {
241233
let greatestGreatestLineProduct = 0
242234
for (let i = 0; i < matrixToTest.length; i++) {
@@ -260,7 +252,32 @@ function greatestProduct(matrixToTest) {
260252
}
261253
}
262254

263-
console.log(greatestProduct(matrix))
255+
//Bonus - Iteration #8.1: Product of diagonals
256+
//with another approach
257+
function greatestProductOfDiagonals(matrixToTest) {
258+
let greatestPartialProductOfDiagonals = 0
259+
let partialProductOfDiagonalRight = 0
260+
let partialProductOfDiagonalLeft = 0
261+
262+
for (let i = 0; i < matrixToTest.length - 3; i++) {
263+
for (let j = 0; j < matrixToTest.length - 3; j++) {
264+
partialProductOfDiagonalRight =
265+
matrixToTest[i][j] * matrixToTest[i + 1][j + 1] * matrixToTest[i + 2][j + 2] * matrixToTest[i + 3][j + 3]
266+
partialProductOfDiagonalLeft =
267+
matrixToTest[i][j + 3] * matrixToTest[i + 1][j + 2] * matrixToTest[i + 2][j + 1] * matrixToTest[i + 3][j]
268+
if (partialProductOfDiagonalRight > greatestPartialProductOfDiagonals) {
269+
greatestPartialProductOfDiagonals = partialProductOfDiagonalRight
270+
}
271+
if (partialProductOfDiagonalLeft > greatestPartialProductOfDiagonals) {
272+
greatestPartialProductOfDiagonals = partialProductOfDiagonalLeft
273+
}
274+
}
275+
}
276+
277+
return greatestPartialProductOfDiagonals
278+
}
279+
280+
console.log('greatestProductOfDiagonals', greatestProductOfDiagonals(matrix))
264281

265282
// The following is required to make unit tests work.
266283
/* Environment setup. Do not modify the below code. */

0 commit comments

Comments
 (0)