1 回答

TA贡献1847条经验 获得超7个赞
你需要reset boxMax到16,因为它成为0在randomBoxes因
boxMax--;
Hide code snippet
let shuffledBoxes = [];
let boxes = []
let boxMax = 16;
let boxCount = 0;
const newGame = () => {
for (let i = boxes.length; i != boxMax; i++) {
if (boxes.length === boxMax) {
return null;
}
if (boxCount != Math.floor(boxMax * 0.4)) {
if (boxes.includes(2) === false) {
boxes.push(2);
}
boxes.push(1);
boxCount++;
} else {
boxes.push(0);
}
}
};
const randomBoxes = () => {
for (let i = boxes.length - 1; i >= 0; i--) {
math = Math.floor(Math.random() * boxMax);
shuffledBoxes.push(boxes[math]);
boxes.splice([math], 1);
boxMax--;
}
};
const boxesClear = () => {
for (let i = shuffledBoxes.length - 1; i > -1; i--) {
shuffledBoxes.pop();
}
for (let j = boxes.length - 1; j > -1; j--) {
boxes.pop();
}
boxCount = 0;
boxMax = 16
};
newGame();
randomBoxes();
boxesClear();
//After this point, newGame does not like to run.
newGame();
randomBoxes();
console.log(shuffledBoxes);
console.log(boxes);
您也可以缩短boxesClear功能
const boxesClear = () => {
shuffleBoxes = [];
boxes = [];
boxCount = 0;
};
您也可以使用一个衬管。
const boxesClear = () => shuffleBoxes.length = boxes.length = boxCount = 0
添加回答
举报