Rock, Paper, Scissors - What could go wrong? (Syntax error)

Hi! I’m having trouble identifying the error in my code. Code and error message(s) posted below.

I’m also having trouble understanding the error message itself. I thought that an error message such as (vm.js:53:10) meant that there was an error on line 53, character 10. However, I have no code on that line. Please advise (as if I’m five).

Warm thanks,

ailanthus
ps: This is my first post in a Codecademy forum. :grimacing:

Rock Paper Scissors

The code:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper') {
    return userInput
  } else {
console.log('Error, please type: rock, paper, or scissors');
  }
}
const getComputerChoice = () => {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
  }
};
const determineWinner = (userChoice, computerChoice) => {
 if (userChoice === computerChoice) {
 return 'This game is a tie!';
}
};

if (userChoice === 'rock') {
  if (computerChoice === 'paper') {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
  }
  
if (userChoice === 'rock') {
  if (computerChoice === 'paper') {
    return "Sorry, computer won!";
  } else {
    return "Congratulations, you won!";
  }
  }
if (userChoice === 'paper') {
    if (computerChoice === 'scissors')
    return "Sorry, computer won!";
      } else {
        return "Congratulations, you won!";

if (userChoice === 'scissors') {
  if (computerChoice === 'rock')
  return "Sorry, computer won!";
      } else {
        return "Congratulations, you won!";
}


console.log(determineWinner('rock', 'scissors'));
console.log(determineWinner('paper', 'scissors'));
console.log(determineWinner('rock', 'rock'));

`
The Error Message:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:59
      });
       ^
SyntaxError: Unexpected token )
    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)```

Hey, I’ve reached the same part of the course so I’m not too sure but maybe the case needs a break after the return to close it (In the getComputerChoice function).

1 Like

The last two if statements are missing some braces.

The switch cases use return so do not need break, so that suggestion may be disregarded.

2 Likes

Unexpected token means that you have a token (like brace, bracket, semicolon, comma etc.) where it doesn’t belong. It also shows you in which line that occurs. You can’t always rely on the line because if you have a scoping problem (like you forgot to close a function) the program doesn’t know where you want the scope to end.

Check your opening and closing curly braces: There are many errors with not correctly opening and closing braces.

2 Likes

Thanks, all! It hadn’t occurred to me that if I mess up the scope, the error message won’t be able to accurately tell me where the error has occurred.

I’ve fixed the problem in my code but that’s by copying the video. I will now go back and look at your suggestions and see where I went astray.

1 Like