9. Next Steps . Rock , paper, scissor. What if its wrong?


#1

hey guys, I've been trying to execute when the user choice differs from our choices.

var userChoice = prompt("Do you choose rock, paper or scissors?");

var goodChoice = function (userChoice) {
if (userChoice !== "rock" ||choice !== "paper" ||choice!== "scissor") { return "sorry , please choose rock, paper or scissor"; }
};

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 === choice2) { return "The result is a tie!";

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

if(choice2 === "rock") { return "rock wins"; }
else { return "scissors wins"; }

}

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

     if(choice2 === "paper") { return"paper wins";}
     else { return "rock wins"; }

}
else if (choice1 === "paper") {
if(choice2=== "rock") { return "paper wins"; }else { return "scissor wins"; }
}

};

compare(userChoice, computerChoice);


#2

You could take this and add it to your compare function as an else if. Make sure to change all your 'choice' to 'userChoice' in the if condition.


#3

Thanks a lot!!!!! didnt tought about that! It was really usefull!


#4

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