@@ -211,28 +211,37 @@ function greatestProduct(numbersArray) {
211
211
let verticalMultiply = 0
212
212
let horizontalMultiply = 0
213
213
for ( let i = 0 ; i + 3 < numbersArray . length ; i ++ ) {
214
- for ( let j = 0 ; j < numbersArray [ i ] . length ; j ++ ) {
215
- for ( let k = 0 , multiplyResult = 1 ; k <= 3 ; k ++ ) {
216
- multiplyResult = ( numbersArray [ i + k ] [ j ] ) * multiplyResult
217
- //console.log(`iteraccion ${k} -> multiplyResult = ${multiplyResult}`)
218
- if ( multiplyResult > verticalMultiply ) {
219
- verticalMultiply = multiplyResult
214
+ if ( numbersArray . length < 4 || numbersArray [ i ] . length < 4 ) {
215
+ return `The array must have at least a 4x4 matrix`
216
+ } else {
217
+ for ( let j = 0 ; j < numbersArray [ i ] . length ; j ++ ) {
218
+ for ( let k = 0 , multiplyResult = 1 ; k <= 3 ; k ++ ) {
219
+ multiplyResult = ( numbersArray [ i + k ] [ j ] ) * multiplyResult
220
+ //console.log(`iteraccion ${k} -> multiplyResult = ${multiplyResult}`)
221
+ if ( multiplyResult > verticalMultiply ) {
222
+ verticalMultiply = multiplyResult
223
+ }
220
224
}
225
+ //console.log(`////`)
221
226
}
222
- //console.log(`////`)
223
227
}
228
+
224
229
}
225
230
226
231
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
232
+ if ( numbersArray . length < 4 || numbersArray [ i ] . length < 4 ) {
233
+ return `The array must have at least a 4x4 matrix`
234
+ } else {
235
+ for ( let j = 0 ; j + 3 < numbersArray [ i ] . length ; j ++ ) {
236
+ for ( let k = 0 , multiplyResult = 1 ; k <= 3 ; k ++ ) {
237
+ multiplyResult = ( numbersArray [ i ] [ j + k ] ) * multiplyResult
238
+ //console.log(`iteraccion ${k} -> multiplyResult = ${multiplyResult}`)
239
+ if ( multiplyResult > horizontalMultiply ) {
240
+ horizontalMultiply = multiplyResult
241
+ }
233
242
}
243
+ //console.log(`////`)
234
244
}
235
- //console.log(`////`)
236
245
}
237
246
}
238
247
//console.log(`verticalMultiply = ${verticalMultiply}`)
0 commit comments