I'm stuck! keep getting unexpected token else. Can someone look at it for me?


#1


7. What if choice1 is paper?


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

Remove the semi colon and put an opening bracket { on this line.

And then add one more closing bracket after the else closing bracket. :slight_smile:


#3

I tried....didn't seem to work ...


#4

If there are multiple mistakes you'll have to fix multiple things!

Going by "works" or not might not be the best approach. Instead look at what you're inspecting and compare to what it should be.

If you want to go by if something "works" or not then you'll have to isolate it from everything else first in a minimal example. It might still not be a good idea because sometimes something behaves similarly to what you want but not exactly, and you might end up with a bug because it "worked" when you wrote it, but later on only "works" sometimes.

You could use a different interpreter (your browser/some online js interpreter/nodjs) to run your code and get a more verbose error message that includes a line number for where the error message was reported so that you know where to look. Another approach is to remove the code (copy it elsewhere) and paste it back one small piece at a time until the problem occurs again, then you know that the last part you pasted back contains the syntax error.


#5

I don't freakin get it either. Here's my code...HELP

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

    if (choice2 === "rock") {
        return "paper wins";


}  } else {
        return "scissors wins";
  }

};


#6

I had the same issue, same code, thought I was following the formatting and syntax which worked for the first if/else statement exactly, so I wasn't (and still am not) sure why what you have doesn't work.

After playing around with brackets and such repeatedly this worked for me:

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

}
}
};

Maybe it was the additional bracket before the 2nd if/else? (sorry formatting sux, bad paste)


#7

remove one closing bracket here


#8

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