Completely stuck... if/else statement as unexpected token?

so I’m getting a syntax error (pasted below), I cannot figure out what I’m doing wrong. I’ve watched the walkthrough video and cannot figure out where the deviation from the video syntax are.

const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (getUserChoice === ‘rock’ || getUserChoice === ‘paper’ || getUserChoice === ‘scissors’);{
return userInput;
} else {
console.log(‘Error, please type Rock, Paper, or Scissors’);
}
};

console.log(getUserChoice(‘paper’));

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:5
} else {
^^^^
SyntaxError: Unexpected token else
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
Welcome to the Get Help category!

This is where you can ask questions about your code. Some important things to remember when posting in this category :slight_smile:

  • Learn how to ask a good question and get a good answer!
  • Remember to include a link to the exercise you need help with!
  • If someone answers your question, please mark their response as a solution :white_check_mark:
  • Once you understand a new concept, come back and try to help someone else!

Hello, and welcome to the forums!

You have a stray semicolon ; at the end of your if condition, so it’s ending it prematurely. This is why it wasn’t expecting the else.

Also, take a closer look at the name of the variable you’re comparing in your if.

More details if you need it

This is where your extra semicolon is
=== ‘scissors’); {

You’re comparing getUserChoice in the if (which is the name of your function) rather than the userInput parameter you mean to use.

2 Likes

The thing to understand here can broken into two parts.

  1. End-of-statement token, the semi-colon. This terminates parsing and passes the parsed code (from the start of the line) to the interpreter. The if body is never parsed, so doesn’t exist in that phase of execution.
  2. Block containment, the {} that accompanies a construct or function. Blocks execute immediately which in this case means that there is no condition allowing or disallowing it. It just runs.
1 Like

thank you! these are always the problems I end up having to put it down I get so frustrated looking trying to troubleshoot

1 Like

You are not alone. It’s one of the most common beginner errors we see. Use of semi-colons is a question that comes up often.The real truth is that JS actually inserts them for us so we don’t technically need them, ever, in our code. It is nonetheless an important syntax to understand thoroughly if you wish to go into C, PHP, Java, et al. JS, not such a big deal.

It’s important to consider line parsing. JS parses line by line, for the most part. A line break is akin to an end-of-statement.

Ping us back if you want to go into greater detail of when and when not to use ;.

sure I think I’d like more info… but I’ll be honest the statement about line parsing might as well be written in a different language cause I have no clue what that means.

It takes years to gain any command of the English language, let alone programming languages. They both have one thing in common… Syntax.

parse

verb: to analyze (a sentence) into its parts and describe their syntactic roles.