如何仅修复第一个警报框显示

how to fix only first alertbox shows

我正在制作一个颜色生成器选择游戏,但我不确定为什么只有循环的第一个警报会永远被拾取而忽略所有其他警报。我已经尝试重新编写它两次并在每个警报之后放置 return。

COLOR_ARRAY = ['blue', 'cyan', 'gold', 'gray', 'green', 'magenta', 'orange', 'red', 'white', 'yellow'];

function runGame() {
  let guess = "";
  let correct = false;

  const targetIndex = Math.floor(Math.random() * COLOR_ARRAY.length);
  const target = COLOR_ARRAY[targetIndex];
  console.log(targetIndex, target);

  do {
    guess = prompt('I am thinking of one of these colors:\n\n' + COLOR_ARRAY + '\n\n What color am I thinking of ?\n');

    if (guess === null) {
      alert('bye');
      return;
      correct = checkGuess(guess, target);
    }
  } while (!correct);
  alert('congratulations!');
}


function checkGuessing(guess, target) {
  let correct = false;
  let right = true;
  if (COLOR_ARRAY.includes(guess)) {
    alert('Color not recognized.');
  } else if (guess > target) {
    alert('Guess incorrect\n\n higher than target.');
  } else if (guess < target) {
    alert('Guess is incorrect...Lower than target.');
  } else {

  }

  return right;
}

runGame();

  1. 错误的函数名称 checkGuessing 而不是 checkGuess
  2. checkGuessing检查猜测是否有效的逻辑错误
  3. 仅在用户取消提示时调用
  4. 没有检查是否 target == guess

COLOR_ARRAY = ['blue', 'cyan', 'gold', 'gray', 'green', 'magenta', 'orange', 'red', 'white', 'yellow'];

function runGame() {
  let guess = "";
  let correct = false;

  const targetIndex = Math.floor(Math.random() * COLOR_ARRAY.length);
  const target = COLOR_ARRAY[targetIndex];
  console.log(targetIndex, target);

  do {
    guess = prompt('I am thinking of one of these colors:\n\n' + COLOR_ARRAY + '\n\n What color am I thinking of ?\n');

    if (guess === null) {
      alert('bye');
      return;
    }
    
    checkGuess(guess, target);
    
    if (guess == target) {
        correct = true;
    }
  } while (!correct);
  alert('congratulations!');
}


function checkGuess(guess, target) {
  if (!COLOR_ARRAY.includes(guess)) {
    alert('Color not recognized.');
  }
  
  if (guess > target) {
    alert('Guess incorrect\n\n higher than target.');
  }

  if (guess < target) {
    alert('Guess is incorrect...Lower than target.');
  }
}

runGame();