For loop and matrix iteration issues : TypeError: Cannot read property '0' of undefined
For loop and matrix iteration issues : TypeError: Cannot read property '0' of undefined
编译我的代码时出现
错误
TypeError: Cannot read property '0' of undefined
我知道这个错误本质上意味着代码试图提取一个不存在的数组元素。这通常是因为有一个 for 循环递增而没有任何终止边界。然而,across++
有 for(let across=0; across<matrix[0].length; across++){
的界限
我想知道是什么导致了错误信息。
Context/Task:
After becoming famous, the CodeBots decided to move into a new building together. Each of the rooms has a different cost, and some of them are free, but there's a rumour that all the free rooms are haunted! Since the CodeBots are quite superstitious, they refuse to stay in any of the free rooms, or any of the rooms below any of the free rooms.
Given matrix, a rectangular matrix of integers, where each value represents the cost of the room, your task is to return the total sum of all rooms that are suitable for the CodeBots (ie: add up all the values that don't appear below a 0).
Example:
matrix =
[[0, 1, 1, 2], [0, 5, 0, 0], [2, 0, 3, 3]]
the output should be
solution(matrix) = 9.
function solution(matrix) {
let count = 0;
for(let across=0; across<matrix[0].length; across++){
for(let down=0; down<matrix.length; down++){
if(matrix[across][down] == 0){
across++;
}else{
count += matrix[across][down];
}
}
}
return count;
}
//[ [1, 1, 1, 0], [0, 5, 0, 1], [2, 1, 3, 10] ]
// 0 1 2
总而言之,您不小心调换了数组访问中的行和列。将各种数组访问代码更改为:
matrix[down][across]
调试注意事项:错误消息提示您使用 matrix[across][down]
的地方,一定是 matrix[across]
的值 undefined
和 down
的值值 0
。这就是导致错误的原因:
TypeError: Cannot read property '0' of undefined
发生这种情况是因为 across
超出了范围。 JavaScript 不进行范围检查,在这种情况下只会产生 undefined
。如果你的矩阵恰好有 3 行和 3 列,你就不会看到错误(尽管它本来会在那里,等待将来失败)。
编译我的代码时出现
错误TypeError: Cannot read property '0' of undefined
我知道这个错误本质上意味着代码试图提取一个不存在的数组元素。这通常是因为有一个 for 循环递增而没有任何终止边界。然而,across++
有 for(let across=0; across<matrix[0].length; across++){
的界限
我想知道是什么导致了错误信息。
Context/Task:
After becoming famous, the CodeBots decided to move into a new building together. Each of the rooms has a different cost, and some of them are free, but there's a rumour that all the free rooms are haunted! Since the CodeBots are quite superstitious, they refuse to stay in any of the free rooms, or any of the rooms below any of the free rooms. Given matrix, a rectangular matrix of integers, where each value represents the cost of the room, your task is to return the total sum of all rooms that are suitable for the CodeBots (ie: add up all the values that don't appear below a 0). Example: matrix =
[[0, 1, 1, 2], [0, 5, 0, 0], [2, 0, 3, 3]]
the output should be solution(matrix) = 9.
function solution(matrix) {
let count = 0;
for(let across=0; across<matrix[0].length; across++){
for(let down=0; down<matrix.length; down++){
if(matrix[across][down] == 0){
across++;
}else{
count += matrix[across][down];
}
}
}
return count;
}
//[ [1, 1, 1, 0], [0, 5, 0, 1], [2, 1, 3, 10] ]
// 0 1 2
总而言之,您不小心调换了数组访问中的行和列。将各种数组访问代码更改为:
matrix[down][across]
调试注意事项:错误消息提示您使用 matrix[across][down]
的地方,一定是 matrix[across]
的值 undefined
和 down
的值值 0
。这就是导致错误的原因:
TypeError: Cannot read property '0' of undefined
发生这种情况是因为 across
超出了范围。 JavaScript 不进行范围检查,在这种情况下只会产生 undefined
。如果你的矩阵恰好有 3 行和 3 列,你就不会看到错误(尽管它本来会在那里,等待将来失败)。