我有一个函数,它应该将(n * n)网格作为参数并输出4 nos的最大乘积。所有行。(项目欧拉问题11的一部分)。当我尝试运行它给我的代码时,
TypeError: Cannot read property 'length' of undefined
我在这里做错了什么?(我是初学者,所以如果我有任何愚蠢的错误,请告诉我。
这是我的代码:
const grid = [
[40, 17, 81, 18, 57],
[74, 4, 36, 16, 29],
[36, 42, 69, 73, 45],
[51, 54, 69, 16, 92],
[7, 97, 57, 32, 16]
];
function largestGridProduct(arr) {
let product = 1 , maxProduct = 1;
for(let i=0 ; i<arr.length ; i++){
for(let j=0 ; j<arr.length-3 ; j++){
product = grid[i][j] * grid[i][j+1] * grid[i][j+2] * grid[i][j+3];
if(product > maxProduct){
maxProduct = product;
}
}
}
return maxProduct;
}
console.log(largestGridProduct(grid));
那么我在这里做错了什么?
米脂
慕婉清6462132
DIEA
相关分类