What if choice1 is paper - stuck


#1

I am currently stuck on this phase and I am getting an error message of:

SyntaxError: Unexpected token else

Here is my code. Please help me figure out what I'm doing wrong here.

var compare = function(choice1,choice2){
if (choice1 === choice2) {
   return "The result is a tie!"; 
}
else if (choice1 === "rock"){
    if (choice2 === "scissors"){
      return "rocks wins";  
    }
};
else  {
    return "paper wins";
}
else if (choice1 === "paper"){
    if (choice 2 === "rock"){
        return "paper wins";
    }
}
else {
    return "scissors wins";
};
};

#2

@lyricyst,

Please read
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
and concentrate on:

  • the creation of the compare function
  • the truth-table
  • the build of the IF ELSE-IF statement structure as part of the compare FUNCTION-BODY
  • the execution of the compare function (keeping in mind that the return statement is used )

#3

No semicolons after conditions see this link for an explanation:

else if (choice1 === "rock"){
    if (choice2 === "scissors"){
      return "rocks wins";  
    }
}; <-- this is a problem

also if you end the else if here, the else becomes the alternative to the else if but it should be the alternative for the inner if.


#4

@leonhard_wettengmx_n @haxor789 After reading both of your posts, I was able to figure out what I was doing incorrectly. I didn't nest the else statement inside the else if braces and I only need one semi-colon after the last closing brace. The code works now! Thanks for your answers.

if anyone else is wants to take a look at the correct code, see below


var compare = function (choice1, choice2) {
if (choice1 === choice2) {
    return "The result is a tie!";
}
else if (choice1 === "rock") {
    if (choice2 === "scissors") {
        return "rocks wins";
    } else {
        return "paper wins";
    }
}
else if (choice1 === "paper") {
    if (choice2 === "rock") {
        return "paper wins";
    } else {
        return "scissors wins";
    }
}
};

#5

@lyricyst,
and you use the semi-colon-; as you defined a variable like

var compare = function(choice1,choice2) { };