Why does the debugger return this error in the Rock, Paper and Scissors game?

In the Rock, Paper and Scissors game, I kept getting the same error when I would run this code:

function determineWinner (userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'Tie';
  } 
  else if (userChoice === 'rock') {
      if (computerChoice === 'paper') {
        return 'Computer wins!';
      } else {
        return 'You win!';
      }
  else if (userChoice === 'paper') {
      if (computerChoice === 'scissors') {
        return 'Computer wins!';
      } else {
        return 'You win!';
      }
  else (userChoice === 'scissors') {
      if (computerChoice === 'rock') {
        return 'Computer wins!';
      } else {
        return 'You win!';
      }
  }
}

The console would return the following error:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:75
});
 ^
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)

It says “line 75”, but I only had 74 lines, and the code causing the error was 20 lines above the last line I’d typed. I had to watch a video to figure out what I did wrong, and it turns out that my nesting brackets were incorrect. Here is the successful code:

function determineWinner (userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'Tie';
  } 
  if (userChoice === 'rock') {
      if (computerChoice === 'paper') {
        return 'Computer wins!';
      } else {
        return 'You win!';
      }
  }
  if (userChoice === 'paper') {
      if (computerChoice === 'scissors') {
        return 'Computer wins!';
      } else {
        return 'You win!';
      }
  }
 if (userChoice === 'scissors') {
      if (computerChoice === 'rock') {
        return 'Computer wins!';
      } else {
        return 'You win!';
      }
  }
}

My question is, why did my code generate an error saying there was an unexpected token on line 75, when line 75 didn’t exist, and the error was actually many lines above? Are nesting problems known to cause this error?

That doesn’t look right.

1 Like