function playerSelection() {
let choice = prompt("Enter you choice.");
while(choice != "rock" && choice != "paper" && choice != "scissor"){
alert("Wrong entry");
playerSelection();
}
return choice;
}
This code works just fine until you enter a string that makes the while loop condition true. Once it enters the while loop after an incorrect entry and prompts you to enter another string even if you enter a correct string, it keeps displaying the "wrong entry" alert and shows the prompt until you reload the page. What is going on?
I tried manipulating the logical operator but that does not seem to help. My guess is it has something to do with the recursion, but I have no clue what it is.
3
Answers
it’s because each new call to
playerSelection()
creates a new call context that stores its own local variables and parameters. try this out:or if you want with recursion
You don’t need to call the function again. The "prompt"-method already does what you (as I am guessing) want to do.
Do this instead:
Your variable should be declared before the function as here :