function getHumanChoice() {
let humanChoice = prompt('Choose rock, paper, or scissors:');
if (humanChoice.toLowerCase() !== humanChoice){
humanChoice = humanChoice.toLowerCase()
}
while (humanChoice !== 'rock' && humanChoice !== 'paper' && humanChoice !== 'scissors') {
alert('Please enter a valid choice');
humanChoice = prompt('Choose rock, paper, or scissors:');
}
return humanChoice
}
I get a typeError in the console whenever I try to close the prompt.
since the error is caused by executing toLowerCase() to null i tried to solve it by an if statement, i got rid of the error but the prompt keeps showing up and console doesnt do anything anymore.
if(humanChoice === null){
return null
} else {
if (humanChoice.toLowerCase() !== humanChoice){
humanChoice = humanChoice.toLowerCase()
}
while (humanChoice !== 'rock' && humanChoice !== 'paper' && humanChoice !== 'scissors') {
alert('Please enter a valid choice');
humanChoice = prompt('Choose rock, paper, or scissors:');
}
return humanChoice
}
2
Answers
You can keep everything in a functionWe need to breack the loop, not by the promp exit button but by using
breack;
. We have to do this becuase are loop is forever unless we change it.Also using your suggested solution would not work as the
if
statment is not in our loop plus we do not use.toString
to get our true undiefind value.Instead of using truthy/falsey directly in the while loop, use this: