Unexpected Token Else


#1



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


I put the else in exactly as they show in the hint, but it says it is unexpected. I only added one else to the code, so it has to be that one. I'm confused.


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

Hi this part you forgot to close the else if statement

else if (choice1 === "rock") {

    if (choice2 === "scissors") {
        return "rock wins";
    } else {
        return "paper wins";
    }
}<== add one here

#3

I did but that didn't work, the console says the problem is the else.


#4

is your code like that

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";
        }
    } <= you add one here 
    else if (choice1 === "paper") {        
        if (choice2 === "rock") {
            return "paper wins";
        }
        else { 
            return "scissors wins"; 
        }
    }
}

#5

I dont understand, what do you mean?


#6

Did you add the } like I said and it didn't work ?
if so can you post your new code?


#7

Ok, here it is:
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"; 
}
}
}

#8

you should first add one } to close the first else if statement then remove one } at the end of your code you have more than you need

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";
    } 
  } <== add one here
    else if (choice1 === "paper") {

      if (choice2 === "rock") {
        return "paper wins";
      }
    } else {
      return "scissors wins";
    }
  }
} <= remove this one

#9

Ok, I did that, but it is still telling me that I have an unexpected token else.


#10

can you take a screenshot of your code?


#11

no, I am on my computer, but i'll copy and paste it.


#12

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

#13

Hi try to copy tha one

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

I remove the one { after the closing if statement

else if (choice1 === "paper") {

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

#14

Thank you very much! How are you so good at this? :grin:


#15

your welcome, I'm a bit good at it!