7. Help me please! (Build Rock Paper Scissors)


#1


I really can't figure out what's wrong with this, please help me! (Also could someone please explain what my error message means?)


https://discuss.codecademy.com/c/javascript/javascript-beginner-en-Bthev-mskY8


This is the error code I got:
SyntaxError: Unexpected token else


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

This error message indicates that one of the else statements is in the wrong place -> it is not placed after if or else if block.

This is your code with proper formatting:

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

}

And we see that after the else block we have else if (choice1 === "paper") which is wrong because else ends the if ... else if ... else construction.

Take a look at indentations and try to fix the problem. Hint, this else is misplaced:

else {
    return "paper wins";
}

#3

Hi @methodslayer74423,

If t may help please see the code example below.
In short the error occurred because you had a closing brace a little too early for your if/else if script blocks. By having the brace too early you had technically made the logic of the function and the order of the if / else if go beyond the normal bounds of how it expected to act and how it could perform logically.
So On the one hand your logic for your aim was great, but unfortunately with the brace closure a little early this made the logic for how JS performs an if / else if go wonky :slight_smile:

With that in mind, and the code example below, the error is trying to inform you that you had an "ELSE" script block in a palce it did not expect EG: at the very end, since one can have many else if, but only one if and one else in the same logical statement of code :slight_smile:

Hopefully that helps a little? Please feel free to reply if that's not clear and someone will try to help as soon as they can :slight_smile:

var compare = function(choice1,choice2)
{
    if(choice1 === choice2) 
    {
        return "The result is a tie!";
    }
    else if(choice1 === "rock")
    {
        if(choice2 === "scissors") 
        {
            return "rock wins" ;            
        }
    //} you have this closing brace in the wrong place. By having this here you have closed your else if a little early
        else //by closing the else if early this would technically be OK since it is an else but...
        {
            return "paper wins"; 
        }
    } //let's move your closing brace here        
    else if(choice1 === "paper")  // now you have another else if after an else and that confuddles JS
    {
        if(choice2 === "rock") 
        {
            return "paper wins" ;
        }
    //} you have this closing brace in the wrong place. Now the error is cascading down your code from the brace closure above
        else 
        {
            return "scissors wins";
        }    
    }
} // and one more to close your function out


#4

Thanks for the help. But what do I put after instead of "else"?


#5

The code sample as pasted above should be everything you need for this lesson.
The comments were simply intended to try and show what changes were made and why. :slight_smile:

Else is pretty much the final conditional check for if /else statements. So you start with the if, you can have as many else if's as needs be, and close with a singular else to finish

If i misunderstood your question my apologies, please let me know. :slight_smile:


#6

what are the value for the parameters choice1 and 2 bro?


#8

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