@@ -46,7 +46,7 @@ function sum(arrayOfEverything) {
46
46
let total = 0 ;
47
47
for ( let element of arrayOfEverything ) {
48
48
if ( typeof ( element ) === 'array' || typeof ( element ) === 'object' ) {
49
- throw new Error ( 'invalid input' )
49
+ throw new Error ( `Unsupported data type sir or ma'am` ) ;
50
50
}
51
51
else {
52
52
if ( typeof ( element ) === 'string' ) {
@@ -229,9 +229,49 @@ const matrix = [
229
229
[ 1 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 1 , 89 , 19 , 67 , 48 ]
230
230
] ;
231
231
232
- function greatestProduct ( ) { }
232
+ function greatestProduct ( matrix ) {
233
+ let productHorizontal = 0 ;
234
+ let productVertical = 0 ;
235
+ let maxResult = 0 ;
236
+
237
+ for ( let element = 0 ; element < matrix . length ; element ++ ) {
238
+ for ( let i = 0 ; i < matrix [ element ] . length ; i ++ ) {
239
+ // Horizontal
240
+ if ( i + 1 >= matrix [ element ] . length || i + 2 >= matrix [ element ] . length || i + 3 >= matrix [ element ] . length ) {
241
+ console . log ( 'Part horizontal iF statment' ) ;
242
+ }
243
+ else {
244
+ productHorizontal = matrix [ element ] [ i ] * matrix [ element ] [ i + 1 ] * matrix [ element ] [ i + 2 ] * matrix [ element ] [ i + 3 ] ;
245
+ console . log ( `Horizontal: ${ matrix [ element ] [ i ] } * ${ matrix [ element ] [ i + 1 ] } * ${ matrix [ element ] [ i + 2 ] } * ${ matrix [ element ] [ i + 3 ] } ` ) ;
246
+ }
247
+
248
+ //Vertical
249
+ if ( element + 1 >= matrix . length || element + 2 >= matrix . length || element + 3 >= matrix . length ) {
250
+ console . log ( 'Part 1 of iF statment' ) ;
251
+ }
252
+ else {
253
+ productVertical = matrix [ element ] [ i ] * matrix [ element + 1 ] [ i ] * matrix [ element + 2 ] [ i ] * matrix [ element + 3 ] [ i ] ;
254
+ console . log ( `Vertical: element: ${ matrix [ element ] [ i ] } * ${ matrix [ element + 1 ] [ i ] } * ${ matrix [ element + 2 ] [ i ] } * ${ matrix [ element + 3 ] [ i ] } ` ) ;
255
+ }
233
256
257
+ //check if any of the products is higher than the last maxResult
258
+ if ( productHorizontal > maxResult || productVertical > maxResult ) {
259
+ if ( productHorizontal > productVertical ) {
260
+ maxResult = productHorizontal ;
261
+ }
262
+ else {
263
+ maxResult = productVertical ;
264
+ } ;
265
+ }
266
+ //Reset productHorizontal and productVertical
267
+ productHorizontal = 0 ;
268
+ productVertical = 0 ;
269
+ }
270
+ }
271
+ return maxResult ;
272
+ }
234
273
274
+ console . log ( greatestProduct ( matrix ) )
235
275
236
276
237
277
// The following is required to make unit tests work.
0 commit comments