Stuck on "What if Choice 1 is Paper"


#1

https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/3?curriculum_id=506324b3a7dffd00020bf661

Hi guys, can't get this code to work properly - it returns me the error message "unexpected token else" - can someone help me out please? I'll be very grateful!

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

Thank you!


#2

the second } closes the else if
You must remove it


#3

Still doesn't quite work after I remove the second } !


#4

Take a look at this part of code:

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

And now the same code written in more readable format:

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

Do you see the problem now? This else statement should be connected to the if (choice2 === "scissors") {, not to else if (choice1 === "rock") {.

The same problem is related to the second part of your code.


#5

Go over your code and closely examine the opening and closing braces. Write closing braces on their own line to help with this process. Identify all the blocks that need demarcation.

... function(...) {
    if ___ {

    }
    else if ___ {
        if ___ {

        } else {

        }
    }
};

#6

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