Skip to content

Commit 93ea188

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

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/functions-and-arrays.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,27 @@ const matrix = [
246246
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
247247
];
248248

249-
function greatestProduct() {}
249+
function greatestProduct(squareMatrix) {
250+
let product = 0;
251+
for (let rowIndex = 0; rowIndex < squareMatrix.length; rowIndex++) {
252+
for (let colIndex = 0; colIndex < squareMatrix.length; colIndex++) {
253+
let tempRowProduct = 0;
254+
let tempColProduct = 0;
255+
if (squareMatrix.length - colIndex >= 4) {
256+
tempRowProduct = squareMatrix[rowIndex][colIndex] * squareMatrix[rowIndex][colIndex + 1] * squareMatrix[rowIndex][colIndex + 2] * squareMatrix[rowIndex][colIndex + 3]
257+
}
258+
if (squareMatrix.length - rowIndex >= 4) {
259+
tempColProduct = squareMatrix[rowIndex][colIndex] * squareMatrix[rowIndex + 1][colIndex] * squareMatrix[rowIndex + 2][colIndex] * squareMatrix[rowIndex + 3][colIndex]
260+
}
261+
if (tempRowProduct > tempColProduct && tempColProduct > product) {
262+
product = tempColProduct;
263+
} else if (tempColProduct > tempRowProduct && tempColProduct > product) {
264+
product = tempColProduct;
265+
}
266+
}
267+
}
268+
return product;
269+
}
250270

251271

252272

0 commit comments

Comments
 (0)