Help with "Rock, Paper, Scissors" please


#1

Hi,
I've been stuck on this one for a while. Just wondering if someone could look at this code and give some advice.
Thanks a bunch.

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";
    }
}

#2

The problem is already in this part:

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

but here it is just a logic error (what if scissors vs paper? -> "paper wins!"...) which becomes a syntax error if you add another else if after the else. So maybe have a look at the hint 6/9.


#3

@javarockstar52458
I think your return "scissors/rock/paper" needs a set of parentheses between "scissors" or whichever element(rock, paper) of the game. so it looks like this; return ("scissors");


#4

Thanks for your reply. Can you explain a bit further?


#5

The else should be an exception to the if choice2 is rock part whereas in your case it is an exception to your else if meaning that anything which is not choice1 === rock will result in here paper wins even if it doesn't involve paper in the first place. The second thing is basically the same as it is analogous to the first.