【问题标题】:Function not working after reset button pressed按下复位按钮后功能不起作用
【发布时间】:2019-04-08 14:36:29
【问题描述】:

“清除”按钮将数组清除回默认值,但之后 newGame 函数拒绝运行。在调用 Clear 函数之前,newGame 函数可以正常工作。我认为新的游戏功能是罪魁祸首,但我不知道它的哪一部分可能会破坏它。

在 newGame 之后应该有一个不同的数组 被第二次调用,但我最终得到一个 无论我调用多少次都是空数组。

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;
};

newGame();
randomBoxes();
boxesClear();
//After this point, newGame does not like to run.
newGame();
randomBoxes();

Logging boxes and shuffledBoxes show that
newGame is working before boxClear is called.

boxes =  []
shuffledBoxes =  [ 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 2, 1, 0, 0, 0, 0 ]
boxCount = 6

boxes =  []
shuffledBoxes =  []
boxCount =  0

【问题讨论】:

    标签: javascript onclick


    【解决方案1】:

    您需要将reset boxMax 转换为16 因为它在randomBoxes 中变为0 由于

    boxMax--;
    

    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
    

    【讨论】:

    • 我实际上是在发布此内容后立即检查的。谢谢你的回答。
    • @Gewerh 我不确定您的代码是否如此。我很确定它可能比原来的要少得多。
    猜你喜欢
    • 2023-01-22
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 2018-10-28
    • 1970-01-01
    • 2021-10-05
    相关资源
    最近更新 更多