gpt4 book ai didi

javascript - JS : Assignment within if statment doesn't work

转载 作者:行者123 更新时间:2023-12-03 08:39:48 25 4
gpt4 key购买 nike

我有一个二维数组,想要将随机元素值设置为 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;

关于javascript - JS : Assignment within if statment doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33082306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com