Search code examples
javascriptwhile-loopdo-whiledo-loops

Javascript: while loop neverending


I've been trying to fix this loop, but apparently it doesn't recognize when the input is right. Am I using the right kind of loop here?

var userChoice = prompt("Do you choose rock, paper or scissors?") 
if (userChoice !== "rock", "paper", "scissors") {
  do {
    prompt("Invalid answer. Please choose rock, paper or scissors.");
  } while (userChoice !== "rock", "paper", "scissors");
} else { 

Solution

  • There are 2 major issues here:

    • You need to re-assign userChoice inside your do-while loop:

      userChoice = prompt("Invalid answer. Please choose rock, paper or scissors.");
      
    • Your comparison is off and won't work, use this instead in your if and while:

      (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors")