What if a user makes an inappropriate choice like 'dog'?


#1

var userChoice = prompt("Do you choose rock, paper, scissors or rope?");
var computerChoice = Math.random();
if (computerChoice <= 0.25) {
computerChoice = "rock";
} else if(computerChoice === 0.26 - 0.50) {
computerChoice = "paper";
} else if(computerChoice === 0.51 - 0.75){
computerChoice = "scissors";
} else {
computerChoice = "rope"
} console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2 ){
if(choice1 === choice2) {
return "The result is a tie!";
}

else if(choice1 === "rock") {

if(choice2 === "scissors") {
    return "Rock wins";
}
else if(choice2 === "paper") {
    return "Paper wins";
}
else if(choice2 === "rope") {
    return "Rope wins";
}
else {
    return "The input is not valid. Please use the correct word in the correct case."
    }

}
else if(choice1 === "paper") {

if(choice2 === "scissors") {
    return "scissors wins";
}
else if(choice2 === "rock") {
    return "Paper wins";
}
else if(choice2 === "rope") {
    return "Rope wins";
}
else {
    return "The input is not valid. Please use the correct word in the correct case."
    }

}
else if(choice1 === "scissors") {

if(choice2 === "paper") {
    return "Scissors wins";
}
else if(choice2 === "rock") {
    return "Rock wins";
}
else if(choice2 === "rope") {
    return "Scissors wins";
}
else {
    return "The input is not valid. Please use the correct word in the correct case."
    }

}
else if(choice1 === "rope") {

if(choice2 === "scissors") {
    return "Scissors wins";
}
else if(choice2 === "paper") {
    return "Rope wins";
}
else if(choice2 === "rock") {
    return "Rope wins";
}
else {
    return "The input is not valid. Please use the correct word in the correct case."
    }

}
};
compare(userChoice, computerChoice)

Why does this not work if the user enters an inappropriate choice?


#2

to ensure the user puts in the right values you might wanna implement handlers to restrict the users input. you could use a while loop to keep prompting the user to enter the right value as long as it does not meet your requirements
Take a look at this post
to give you an idea