循环中“I”之前的 var 遗漏或未捕获的 TypeError:无法读取未定义的属性“length”
2016-09-01
87
我在尝试编写井字游戏时有一些 javascript 代码,但 chrome 开发人员工具给出了此错误
未捕获的类型错误:无法读取未定义的属性“length”
我发现当我在代码末尾注释“isEnd(bigArray[i][j]);”时,它可以工作。
代码:
var inputItem = 'o';
var board = [null, null, null, null, 'o', null, null, null, null];
var sign = {
max: inputItem === "o" ? "x" : "o",
min: inputItem === "x" ? "x" : "o"
};
function generateTable(board, sign) {
var testBoard = [],
nextBoard;
for (var i = 0; i < board.length; i++) {
nextBoard = board.slice();
if (!nextBoard[i]) {
nextBoard[i] = sign;
testBoard.push(nextBoard);
}
}
return testBoard;
}
function isEnd(board) {
console.log('board', board);
for (i = 0; i < board.length; i += 3) {
if (board[i] === board[i + 1] && board[i + 1] === board[i + 2]) {
return board[i] !== null ? board[i] : false;
}
}
for (var j = 0; j < board.length; j++) {
if (board[j] === board[j + 3] && board[j + 3] === board[j + 6]) {
return board[j] !== null ? board[j] : false;
}
}
if ((board[4] === board[0] && board[4] === board[8]) ||
(board[4] === board[2] && board[4] === board[6])) {
return board[4] !== null ? board[4] : false;
}
}
function miniMax() {
var testBoard = generateTable(board, sign.max);
testBoard = testBoard.map(function(elem) {
return generateTable(elem, sign.min);
});
return testBoard;
}
var bigArray = miniMax();
console.log(bigArray);
var maxArray = 0;
var tempArray = 0;
var maxCoord = 0;
for (var i = 0; i < bigArray.length; i++) {
for (var j = 0; j < bigArray[i].length; j++) {
console.log('i, j: ', i, " ", j);
isEnd(bigArray[i][j]);
maxCoord = i;
if (tempArray > maxArray) {
maxCoord = i;
}
}
}
i, j: 0 0 Script snippet #21:22
board ["x", "o", null, null, "o", null, null, null, null] Script snippet #21:57
i, j: 6 1 Script snippet #21:22
board [null, "o", null, null, "o", null, null, "x", null]
2个回答
这是因为在函数中:
function isEnd(board) {
您有此行:
for (i = 0; i < board.length; i += 3) {
因此 i 变量是全局的。
将此行更改为:
for (var i = 0; i < board.length; i += 3) {
示例:
var inputItem = 'o';
var board = [null, null, null, null, 'o', null, null, null, null];
var sign = {
max: inputItem === "o" ? "x" : "o",
min: inputItem === "x" ? "x" : "o"
};
function generateTable(board, sign) {
var testBoard = [],
nextBoard;
for (var i = 0; i < board.length; i++) {
nextBoard = board.slice();
if (!nextBoard[i]) {
nextBoard[i] = sign;
testBoard.push(nextBoard);
}
}
return testBoard;
}
function isEnd(board) {
console.log('board', board);
//
// This is the only line changed
//
for (var i = 0; i < board.length; i += 3) {
if (board[i] === board[i + 1] && board[i + 1] === board[i + 2]) {
return board[i] !== null ? board[i] : false;
}
}
for (var j = 0; j < board.length; j++) {
if (board[j] === board[j + 3] && board[j + 3] === board[j + 6]) {
return board[j] !== null ? board[j] : false;
}
}
if ((board[4] === board[0] && board[4] === board[8]) ||
(board[4] === board[2] && board[4] === board[6])) {
return board[4] !== null ? board[4] : false;
}
}
function miniMax() {
var testBoard = generateTable(board, sign.max);
testBoard = testBoard.map(function(elem) {
return generateTable(elem, sign.min);
});
return testBoard;
}
var bigArray = miniMax();
console.log(bigArray);
var maxArray = 0;
var tempArray = 0;
var maxCoord = 0;
for (var i = 0; i < bigArray.length; i++) {
for (var j = 0; j < bigArray[i].length; j++) {
console.log('i, j: ', i, " ", j);
isEnd(bigArray[i][j]);
maxCoord = i;
if (tempArray > maxArray) {
maxCoord = i;
}
}
}
gaetanoM
2016-09-01
这是因为
isEnd()
中的
board
未定义。检查您发送的内容。
也许您想发送
bigArray
而不是
bigArray[i][j]
。
dlsso
2016-09-01