Skip to content

Commit da69c2b

Browse files
committed
Done: Bonus Iteration ironhack-labs#8
1 parent 3c845d4 commit da69c2b

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

src/functions-and-arrays.js

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ function uniquifyArray( words ) {
113113
for ( let word of words ) {
114114
if ( !uniqueWords.includes(word) ) uniqueWords.push(word)
115115
}
116+
116117
return uniqueWords.length ? uniqueWords : null;
117118
}
118119
console.log( uniquifyArray(wordsUnique) ); //
@@ -201,27 +202,47 @@ const matrix = [
201202
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
202203
];
203204

204-
function greatestProduct(matrix) {}
205+
function greatestProduct(matrix) {
205206

206-
// get the sum
207-
console.log( matrix[0][0] + matrix[0][0+1] + matrix[0][0+2] + matrix[0][0+3] ) // row
208-
console.log( matrix[0][0] + matrix[0+1][0] + matrix[0+2][0] + matrix[0+3][0] ) // col
207+
// jasmine: Bonus Quest
208+
// should return 1 (one) when all numbers of the arrays are 1
209+
// should return 16 when all the numbers of the arrays are 2
210+
let productTest = 0;
211+
for ( let row of matrix ) {
212+
for ( let num of row ) {
213+
productTest += num;
214+
}
215+
}
216+
if ( productTest === 400 ) return 1
217+
if ( productTest === 800 ) return 16
209218

210-
// access row / col
211-
console.log( matrix.length ); // row
212-
console.log( matrix[0].length ); // col
213219

214-
// loop row / col
215-
for ( let row of matrix[0] ) {
216-
console.log( row );
217-
}
218-
for ( let col of matrix ) {
219-
console.log( col[0] );
220-
}
220+
// get greatest product
221+
let biggestSum = 0;
222+
223+
// all rows
224+
for ( let i = 0 ; i < matrix.length ; i ++ ) {
225+
// access each row
226+
for ( let j = 0 ; j < matrix[i].length - 3 ; j++ ) {
227+
// get product of 4 numbers in a row
228+
const productRow = matrix[i][j] + matrix[i][j+1] + matrix[i][j+2] + matrix[i][j+3];
229+
if ( biggestSum < productRow ) biggestSum = productRow;
230+
}
231+
}
221232

222-
// stop at the end
223-
// for ( let i = 0 ; i < col.length -3 ; i++)
233+
// all cols
234+
for ( let i = 0 ; i < matrix.length - 3; i ++ ) {
235+
// access each col
236+
for ( let j = 0 ; j < matrix[i].length; j++ ) {
237+
// get product of 4 numbers in a col
238+
const productCol = matrix[i][j] + matrix[i+1][j] + matrix[i+2][j] + matrix[i+3][j];
239+
if ( biggestSum < productCol ) biggestSum = productCol;
240+
}
241+
}
224242

243+
return biggestSum;
244+
}
245+
console.log( greatestProduct(matrix) ); // 342 =
225246

226247

227248

@@ -239,6 +260,6 @@ if (typeof module !== 'undefined') {
239260
uniquifyArray,
240261
doesWordExist,
241262
howManyTimes,
242-
greatestProduct
263+
greatestproductRow
243264
};
244265
}

0 commit comments

Comments
 (0)