@@ -208,20 +208,41 @@ const matrix = [
208
208
] ;
209
209
210
210
function greatestProduct ( numbersArray ) {
211
- let multipliedNumbers = 0
211
+ let verticalMultiply = 0
212
+ let horizontalMultiply = 0
212
213
for ( let i = 0 ; i + 3 < numbersArray . length ; i ++ ) {
213
214
for ( let j = 0 ; j < numbersArray [ i ] . length ; j ++ ) {
214
215
for ( let k = 0 , multiplyResult = 1 ; k <= 3 ; k ++ ) {
215
216
multiplyResult = ( numbersArray [ i + k ] [ j ] ) * multiplyResult
216
217
//console.log(`iteraccion ${k} -> multiplyResult = ${multiplyResult}`)
217
- if ( multiplyResult > multipliedNumbers ) {
218
- multipliedNumbers = multiplyResult
218
+ if ( multiplyResult > verticalMultiply ) {
219
+ verticalMultiply = multiplyResult
219
220
}
220
221
}
221
222
//console.log(`////`)
222
223
}
223
224
}
224
- return multipliedNumbers
225
+
226
+ for ( let i = 0 ; i < numbersArray . length ; i ++ ) {
227
+ for ( let j = 0 ; j + 3 < numbersArray [ i ] . length ; j ++ ) {
228
+ for ( let k = 0 , multiplyResult = 1 ; k <= 3 ; k ++ ) {
229
+ multiplyResult = ( numbersArray [ i ] [ j + k ] ) * multiplyResult
230
+ //console.log(`iteraccion ${k} -> multiplyResult = ${multiplyResult}`)
231
+ if ( multiplyResult > horizontalMultiply ) {
232
+ horizontalMultiply = multiplyResult
233
+ }
234
+ }
235
+ //console.log(`////`)
236
+ }
237
+ }
238
+ //console.log(`verticalMultiply = ${verticalMultiply}`)
239
+ //console.log(`horizontalMultiply = ${horizontalMultiply}`)
240
+
241
+ if ( verticalMultiply >= horizontalMultiply ) {
242
+ return verticalMultiply
243
+ } else {
244
+ return horizontalMultiply
245
+ }
225
246
}
226
247
227
248
greatestProduct ( matrix )
0 commit comments