Lesson 8 code


#1

Hi,

Now I am having issues with lesson 8 of JavaScript's "rock paper scissors" game. I am getting an error that says: "SyntaxError: Unexpected key word 'else'". Here's the code:

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 === "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";
             } 
    else if (choice1 === "scissors") 
        {
        if (choice2 ==="rock")   {
            return "rock wins"; }
            else {
             return "scissors wins";
                 }
        }
  
  compare(userChoice, computerChoice);

#2

This generally implies a bracket issue.

There is the issue. The third closing brace (RBRACE) is actually closing the function body, exposing the else to the global scope. Remove that third brace and try again. Check for any other braces that are out of place, missing or stray.

Actually, just took a closer look. Move that brace to the bottom (above the call expression, though) and add a semi-colon since it closes an assignment statement.


#3

I'm also getting the same "SyntaxError: Unexpected token else". I've been looking through my code, and I'm not finding an error. I need another pair of eyes.

It's probably super obvious, but I'm getting so sick of looking at this.

Here's all my code:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

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

#4

I see two missing }'s. One before the last else if and the one at the end of the function body. The last one gets a semi-colon since it is closing an assignment statement.


#5

Thanks, mtf, tried that but it's not working. Same error message.


#6

That will cause the Unexpected token else

If you are getting an end of output error, then it is missing the closing brace on following.


#7

That corrected it for me. Thanks!