I have been learning JS on my own for a while using codecademy.com and one task was to make a JS rock, paper, scissors game. At the end it asked for some enhancements, like if it was a tie how would you make it to where you could choose again and if the user had an invalid entry how would you address that problem.
The following is the entire JS game, fairly small. I just need some help on how to get the first if statement in my function to work properly by correctly checking the first parameter and if it is invalid have a prompt that updates the parameter without affecting the second. and then rerun the function. My "OR" operators don't seem to be working either.
Also I would like to know if there is a cleaner/better way to write my second if statement. It works fine I'm just curious if there is a better way.
Thank you all for your help!
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function (choice1, choice2){
if (choice1 !== "rock" || "scissors" || "paper"){
choice1 = prompt("Invalid entry, please reenter")}
if (choice1 === choice2){
var userChoice = prompt("Tie! Choose Again!");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
compare(userChoice, computerChoice);
} else if (choice1 === "rock"){
if(choice2 === "scissors"){
return "rock wins";
}else{
return "Paper wins"};
} else if (choice1 === "paper"){
if (choice2 === "rock"){
return "paper wins";
}else {
return "scissors win"}
} else if (choice1 === "scissors"){
if (choice2 === "rock"){
return "rock wins"
}else {
return "scissors wins"}
};
};
compare(userChoice, computerChoice);