JS:if 语句中的赋值不起作用
JS: Assignment within if statment doesn't work
我有一个二维数组,想将随机元素值设置为 2。
我的尝试如下,但这没有用..
如果随机值未定义,则设置为2,如果不重复。
提前致谢!
var randomTileFunction = function (board) {
var randomTile = board[Math.floor(Math.random() * 4)][Math.floor(Math.random() * 4)];
if(randomTile === undefined) {
randomTile = 2;
} else {
randomTileFunction(board);
}
};
randomTile
是一个变量,其赋值不影响另一个变量 board[Math.floor(Math.random() * 4)][Math.floor(Math.random() * 4)]
.
var i = Math.floor(Math.random() * 4);
var j = Math.floor(Math.random() * 4);
if(board[i][j] === undefined) {
board[i][j] = 2;
我有一个二维数组,想将随机元素值设置为 2。 我的尝试如下,但这没有用.. 如果随机值未定义,则设置为2,如果不重复。 提前致谢!
var randomTileFunction = function (board) {
var randomTile = board[Math.floor(Math.random() * 4)][Math.floor(Math.random() * 4)];
if(randomTile === undefined) {
randomTile = 2;
} else {
randomTileFunction(board);
}
};
randomTile
是一个变量,其赋值不影响另一个变量 board[Math.floor(Math.random() * 4)][Math.floor(Math.random() * 4)]
.
var i = Math.floor(Math.random() * 4);
var j = Math.floor(Math.random() * 4);
if(board[i][j] === undefined) {
board[i][j] = 2;