@@ -211,28 +211,37 @@ function greatestProduct(numbersArray) {
211211 let verticalMultiply = 0
212212 let horizontalMultiply = 0
213213 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+ }
220224 }
225+ //console.log(`////`)
221226 }
222- //console.log(`////`)
223227 }
228+
224229 }
225230
226231 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+ }
233242 }
243+ //console.log(`////`)
234244 }
235- //console.log(`////`)
236245 }
237246 }
238247 //console.log(`verticalMultiply = ${verticalMultiply}`)
0 commit comments