Ad

Writing A Prompt Inside A While-loop

- 1 answer

I want the code to continue, only if the user inputs rock, paper or scissors, but it seems to continue after I re-enter any input the second time, despite it passing the condition in the while loop. For example, if I type "asdf" it will ask me to re-enter the input, but then if I do "asdf" again, it exits the while-loop and logs out "asdf". I want it to keep prompting the user until they enter "rock", "paper" or "scissors"

var userChoice = prompt("Enter rock, paper, or scissors", "rock").toLowerCase();


while (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors" ) {
  userChoice = prompt("renter Please");
}

console.log(userChoice);
Ad

Answer

So if the user reenters more than one time you return false or exit the system.

     var userChoice = prompt("Enter rock, paper, or scissors").toLowerCase();
        while (userChoice != "rock" && userChoice != "paper" && userChoice !=
            "scissors") {
            userChoice = prompt("renter Please");
           
        }
        console.log(userChoice)

Ad
source: stackoverflow.com
Ad