samedi 21 août 2021

how to fix only first alertbox shows

im making a color generator picking game and im not sure why only the first alert for the loop is picked up forever and it ignores all the other ones. i already tried re-writhing it twice and put return after every alert already.

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();

Aucun commentaire:

Enregistrer un commentaire