(My code seems fine, still getting Syntax Error 'got keyword else')


#1

I keep getting error: SyntaxError: expected expression, got keyword 'else' - I've read previous threads and pasted others code all to no avail. Still getting errors. Can anyone see where I'm going wrong here? Is there a { } out of place?

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";
>    }
> }
> console.log(userChoice, computerChoice);

Thanks in advance!


#2

What's with all the >'s? Did you put them there?

You opened a total of 8 curly brackets, but closed only 6. Go along your code again, and look closely. If you click on a curly bracket, it'll show you where it opened (if you click on the closed curly bracket) or where it closed (if you click on the open curly bracket).

Hint: one of the curly brackets you forgot lies in the first else if statement (the entire thing). The second one is at the end of your entire function.


#3

Yes, I inadvertently put those there by using the using the blockquote tool writing this post...they look strange, I agree.

Thanks for the reply. I'm going through the { } now per your suggestion.


#4

Ok. Fixed the 2 open-ended curly brackets. Still getting the error...any other suggestions? Here's the updated 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 === "paper") {
   if(choice2 === "rock") {
       return === "paper wins";
   } 
   else {
        return "scissors wins";
   }
}
console.log(userChoice, computerChoice);

#5

Here:

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

You closed the else if statement and then made an else statement. It should be in your else if statement, after the if inside of it closes. You're now telling it to return 'paper wins' if choice1 is anything but "rock", or if choice1 is rock but choice2 isn't scissors. If choice1 isn't "rock", it'll move onto your else statement and ignore everything after that.
I think it was my fault for being a bit vague on that part, sorry!

Also, here:

var compare = function(choice1,choice2) {
if (choice1 === choice2) {
    return "The result is a tie!";
  }
}

You closed the function after your first if statement, so it doesn't run the other else ifs. It should be on the line before console.log(userChoice, computerChoice);.


#6

'Unexpected token else' is what it says


#7

That is an important piece of information but to help you we probably need to know the exercise name and/or number as well as the code you've written.


#8

Thanks. Its in the "Build Rock, Paper, Scissor" section under "Compare Function" lesson 7. Is that what you were looking for?


#9

I actually meant @arrayjumper79003 sry. This is also a reason why you should not cross post. Rather open a new topic in case of a question about your code.

@legend79 Yes that is helpful for your problem but I guess @seemmetor already gave some good hints.
Or do you have a question concerning them?
The idea is that compare should get valid inputs and should return who won. So any case should be included in the function, for example this:

var compare = function(choice1,choice2) {
if (choice1 === choice2) {
    return "The result is a tie!";
  }
}

only checks for the tie case. It also has the problem that the if starts in the function but the else if corresponding to it is placed outside of the function where there is no connection between them. That's one reason for the unexpected else error. Also for the inner cases you might have a look at the hint of 6/9.


#10

Thanks for your help. Per @seemmetor hint, I updated my closing brackets to close right above:

Here's what the updated code looks like:

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";
   }
}}}
console.log(userChoice, computerChoice);

This appears to have resolved the issue about closing a function too many times. I hope I'm making sense! I feel like I'm lost in the curly bracket farm!

You guys are the best for helping me figure this out. Many thanks.


#11

Unfortunately you still have the problem from the beginning. That the } for the first else if is missing (you matched it at the very end but that is not the place for it). For the curly brackets in general it really helps if you format your code well, meaning add a level of indentation for every new opened { e.g.

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";
       }
     // here the } for the else if is missing
    else if(choice1 === "paper") {
       if(choice2 === "rock") {
           return "paper wins";
       } 
       else {
           return "scissors wins";
       }
    }
}
} // therefore this is now too much.
console.log(userChoice, computerChoice);

#12

Does this look better? Now I'm not getting that same error, but rather a different one saying: SyntaxError: expected expression, got '==='

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";
   }
}
}
console.log(userChoice, computerChoice);

#13

@haxor789 added === to return, most likely by accident.

Should be

return "paper wins";

rather than

return === "paper wins";

#14

easy enough! Thanks! Now just trying to figure out why its saying I haven't defined userChoice. I'll get this one day :smile:


#15

Can you send the entire code (including the part before the compare fuction)?


#16
/*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 === "paper") {
   if(choice2 === "rock") {
       return "paper wins";
   } 
   else {
        return "scissors wins";
   }
}
}
console.log(userChoice, computerChoice);

#17

Ah, I see. You have a /* at the start of your code which ends at } console.log("Computer: " + computerChoice);*/, so the entire part which declares userChoice and computerChoice is a comment and won't actually run. Removing that should do the trick.


#18

AHH it works!! Yes! I thought that may be it. The lesson automatically inserts the /* */ as a way to isolate the compare function. It doesn't clarify they need to be removed at the completion of the lesson segment to move forward.

You guys are the best support community a guy could ask for!


#19

I'm pretty sure that in lesson 8, it tells you to delete the /* and */ :smile: glad to help!


#20

...just saw that. Thanks again @seemmetor and @haxor789