Skip to content

Commit 69e8e16

Browse files
esrfreeesrfree
esrfree
authored and
esrfree
committed
Iteration ironhack-labs#8: Bonus - Horizontal, Vertical and Diagonal adjacents
1 parent f3c50dc commit 69e8e16

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

starter-code/src/functions-and-arrays.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,40 @@ const matrix = [
137137
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
138138
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
139139
];
140+
function greatestProduct( matrix, n ) {
141+
let greatest = [];
142+
143+
//Horizontal adjacents
144+
for ( let a = 0; a < matrix.length; a++ ) {
145+
for ( let i = 0; i <= matrix[a].length - n; i++) {
146+
let prod = 1;
147+
for (let j = i; j < i + n; j++) {
148+
prod *= matrix[a][j];
149+
}
150+
greatest.push(prod);
151+
}
152+
}
153+
154+
// Vertical adjacents
155+
for ( let a = 0; a < matrix.length; a++ ) {
156+
for ( let i = 0; i <= matrix.length - n; i++ ) {
157+
let prod = 1;
158+
for ( let j = i; j < i + n; j++ ) {
159+
prod *= matrix[j][a];
160+
}
161+
greatest.push(prod);
162+
}
163+
}
164+
165+
// Diagonal adjacents
166+
for ( let a = 0; a <= matrix.length - n; a++ ) {
167+
for ( let i = 0; i <= matrix[a].length - n; i++ ) {
168+
let prod = 1;
169+
for ( let j = 0; j < n; j++ ) {
170+
prod *= matrix[a + j][i + j];
171+
}
172+
greatest.push(prod);
173+
}
174+
}
175+
return Math.max(...greatest);
176+
}

0 commit comments

Comments
 (0)