8. if choice 1 is scissors


#1

Hi all,

I've had some issues with this last part of the exercise, this is my code(see below).

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

if (choice1 === "scissors"){
return "scissors wins";
}

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

}
};

when I run my code this is the message I get. - "Your code returned 'scissors wins' instead of 'undefined' when the inputs are scissors and rock"

I don't understand where I've gone wrong with this and have been over this exercise a few times, rewriting parts of it etc and looking back over part 7. Any help or suggestions as to why I 'm getting this message and how I could fix the code would be appreciated.

Thanks in advance.


#2

rock beats scissors

That error message is failing to tell you what the result should be, but it's correct in that that there's something wrong there


#3

you mixed up the names of your parameters in the if / else statement.
this is your code:
if (choice1 === choice2){
return "The result is a tie!";
}
else if( choice1 === "rock"){

if(choice2 === "scissors"){
return "rock wins";
}
working code:
if(choice1 == choice2){
return "The result is a tie!";
}
else if(choice2 == "rock"){
if(choice1 == "scissors"){
return "rock wins"
}
Hope it helps.
-KenDough


#4

Thanks Kendough, I'll go back and check my code.