What if a user makes an inappropriate choice like 'dog'? How can we extend the function to handle that?


#1

What if a user makes an inappropriate choice like 'dog'? How can we extend the function to handle that?

var userChoiceVerification = function() {
var userChoice = prompt("Do you choose rock, paper or scissors?");
if(userChoice === "rock"||"paper"||"scissors"){
console.log("userChoice: " + userChoice);
return userChoice;

    }
else{
    alert("输出错误");
    userChoiceVerification();
    };
};

#2

Hello @byterunner81071,

I would suggest a better way to handle this. In your existing code, you can make an else statement, after your checks for rock, paper and scissors. You don't need to make a whole new function for it, since you won't be using it repeatedly. The form of it could be something like

else {
    prompt("You didn't enter rock, paper or scissors, Try again...")
}

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.