7. What if choice1 is paper?


#1

Hi!

Please, help!
Here's what is says.

Unexpected token.
There's a problem with your syntax.

var compare = function (choice1, choice2)
{ //open function
    if (choice1 === choice2) {
    return "The result is a tie!";
}
else 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

see your code

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

how could it be possible ??i mean you should write

else if (choice2==="scissors") {
if(choice1==="rock"){
console.log( "Rock wins");
}

This is the first step then you have to close the compare function and call it.i hope now you have undestand :slight_smile: .


#3

Hey @anna5555,
as the error says, there's a problem with your syntax.

Check your if-construct - can it really be an if-elseif-else-elseif?


Got stuck? Here the right Version:

I made it a "little bit" more complex (rock, paper, scissors, lizard, spock) - but here the "easy" (required) version:

var compare = function (choice1, choice2) { // Start function
    if (choice1 === choice2) 
    {
        return ("The result is a tie!");
    } 
    else if (choice1 === "rock") 
    { // Start outer if 1
        if (choice2 === "paper") 
        { // Start inner if 1.1
            return ("paper wins!");
        } // End inner if 1.1
        else // In this case it can just be "scissors"
        { // Start inner if 1.2 
            return ("rock wins");
        } // End inner if 1.2 
    } // End outer if 1
    else if (choice1 === "paper") 
    { // Start outer if 2
        if (choice2 === "rock") 
        { // Start inner if 2.1
            return ("paper wins");
        } // End inner if 2.1
        else // This can only be "scissors" aswell
        { // Start inner if 2.2
            return ("scissors wins!");
        } // End inner if 2.2
    } // End outer if 2
    else // the third choice equals - you can guess it - scissors.
    { // Start outer if 3
        if (choice2 === "rock") 
        { // Start inner if 3.1
            return ("rock wins!");
        } // End inner if 3.1
        else 
        { // Start inner if 3.2
            return ("scissors wins!");
        } // End inner if 3.2
    } // End outer if 3
} // End function

I haven't tested the code, cause I just wrote it from scratch again - but it should work!


Hope that I could help you :slight_smile:
If you got any further questions, feel free to ask them.


EDIT: @rcodeman was faster. Had a 24h "block" since I created my discuss-account yesterday :stuck_out_tongue: