@@ -113,6 +113,7 @@ function uniquifyArray( words ) {
113
113
for ( let word of words ) {
114
114
if ( ! uniqueWords . includes ( word ) ) uniqueWords . push ( word )
115
115
}
116
+
116
117
return uniqueWords . length ? uniqueWords : null ;
117
118
}
118
119
console . log ( uniquifyArray ( wordsUnique ) ) ; //
@@ -201,27 +202,47 @@ const matrix = [
201
202
[ 1 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 1 , 89 , 19 , 67 , 48 ]
202
203
] ;
203
204
204
- function greatestProduct ( matrix ) { }
205
+ function greatestProduct ( matrix ) {
205
206
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
209
218
210
- // access row / col
211
- console . log ( matrix . length ) ; // row
212
- console . log ( matrix [ 0 ] . length ) ; // col
213
219
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
+ }
221
232
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
+ }
224
242
243
+ return biggestSum ;
244
+ }
245
+ console . log ( greatestProduct ( matrix ) ) ; // 342 =
225
246
226
247
227
248
@@ -239,6 +260,6 @@ if (typeof module !== 'undefined') {
239
260
uniquifyArray,
240
261
doesWordExist,
241
262
howManyTimes,
242
- greatestProduct
263
+ greatestproductRow
243
264
} ;
244
265
}
0 commit comments