Skip to content

Commit c7afa55

Browse files
committed
Bonus - Iteration ironhack-labs#8.1: Product of adjacent numbers (revision)
1 parent 93ea188 commit c7afa55

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/functions-and-arrays.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ function greatestProduct(squareMatrix) {
258258
if (squareMatrix.length - rowIndex >= 4) {
259259
tempColProduct = squareMatrix[rowIndex][colIndex] * squareMatrix[rowIndex + 1][colIndex] * squareMatrix[rowIndex + 2][colIndex] * squareMatrix[rowIndex + 3][colIndex]
260260
}
261-
if (tempRowProduct > tempColProduct && tempColProduct > product) {
262-
product = tempColProduct;
261+
if (tempRowProduct > tempColProduct && tempRowProduct > product) {
262+
product = tempRowProduct;
263263
} else if (tempColProduct > tempRowProduct && tempColProduct > product) {
264264
product = tempColProduct;
265265
}
@@ -268,6 +268,28 @@ function greatestProduct(squareMatrix) {
268268
return product;
269269
}
270270

271+
function greatestProductDiagonals(squareMatrix) {
272+
let product = 0;
273+
for (let rowIndex = 0; rowIndex < squareMatrix.length; rowIndex++) {
274+
for (let colIndex = 0; colIndex < squareMatrix.length; colIndex++) {
275+
let tempRightDiagProduct = 0;
276+
let tempLeftDiagProduct = 0;
277+
if (squareMatrix.length - colIndex >= 4 && squareMatrix.length - rowIndex >= 4) {
278+
tempRightDiagProduct = squareMatrix[rowIndex][colIndex] * squareMatrix[rowIndex + 1][colIndex + 1] * squareMatrix[rowIndex + 2][colIndex + 2] * squareMatrix[rowIndex + 3][colIndex + 3]
279+
}
280+
if (colIndex - squareMatrix.length >= 4 && rowIndex - squareMatrix.length >= 4) {
281+
tempLeftDiagProduct = squareMatrix[rowIndex][colIndex] * squareMatrix[rowIndex - 1][colIndex - 1] * squareMatrix[rowIndex - 2][colIndex - 2] * squareMatrix[rowIndex - 3][colIndex - 3]
282+
}
283+
if (tempRightDiagProduct > tempLeftDiagProduct && tempRightDiagProduct > product) {
284+
product = tempRightDiagProduct;
285+
} else if (tempLeftDiagProduct > tempRightDiagProduct && tempLeftDiagProduct > product) {
286+
product = tempLeftDiagProduct;
287+
}
288+
}
289+
}
290+
return product;
291+
}
292+
271293

272294

273295

0 commit comments

Comments
 (0)