@@ -258,8 +258,8 @@ function greatestProduct(squareMatrix) {
258
258
if ( squareMatrix . length - rowIndex >= 4 ) {
259
259
tempColProduct = squareMatrix [ rowIndex ] [ colIndex ] * squareMatrix [ rowIndex + 1 ] [ colIndex ] * squareMatrix [ rowIndex + 2 ] [ colIndex ] * squareMatrix [ rowIndex + 3 ] [ colIndex ]
260
260
}
261
- if ( tempRowProduct > tempColProduct && tempColProduct > product ) {
262
- product = tempColProduct ;
261
+ if ( tempRowProduct > tempColProduct && tempRowProduct > product ) {
262
+ product = tempRowProduct ;
263
263
} else if ( tempColProduct > tempRowProduct && tempColProduct > product ) {
264
264
product = tempColProduct ;
265
265
}
@@ -268,6 +268,28 @@ function greatestProduct(squareMatrix) {
268
268
return product ;
269
269
}
270
270
271
+ function greatestProductDiagonals ( squareMatrix ) {
272
+ let product = 0 ;
273
+ for ( let rowIndex = 0 ; rowIndex < squareMatrix . length ; rowIndex ++ ) {
274
+ for ( let colIndex = 0 ; colIndex < squareMatrix . length ; colIndex ++ ) {
275
+ let tempRightDiagProduct = 0 ;
276
+ let tempLeftDiagProduct = 0 ;
277
+ if ( squareMatrix . length - colIndex >= 4 && squareMatrix . length - rowIndex >= 4 ) {
278
+ tempRightDiagProduct = squareMatrix [ rowIndex ] [ colIndex ] * squareMatrix [ rowIndex + 1 ] [ colIndex + 1 ] * squareMatrix [ rowIndex + 2 ] [ colIndex + 2 ] * squareMatrix [ rowIndex + 3 ] [ colIndex + 3 ]
279
+ }
280
+ if ( colIndex - squareMatrix . length >= 4 && rowIndex - squareMatrix . length >= 4 ) {
281
+ tempLeftDiagProduct = squareMatrix [ rowIndex ] [ colIndex ] * squareMatrix [ rowIndex - 1 ] [ colIndex - 1 ] * squareMatrix [ rowIndex - 2 ] [ colIndex - 2 ] * squareMatrix [ rowIndex - 3 ] [ colIndex - 3 ]
282
+ }
283
+ if ( tempRightDiagProduct > tempLeftDiagProduct && tempRightDiagProduct > product ) {
284
+ product = tempRightDiagProduct ;
285
+ } else if ( tempLeftDiagProduct > tempRightDiagProduct && tempLeftDiagProduct > product ) {
286
+ product = tempLeftDiagProduct ;
287
+ }
288
+ }
289
+ }
290
+ return product ;
291
+ }
292
+
271
293
272
294
273
295
0 commit comments