Js rock paper scissors exercise - syntax error

I know that the error is in the last chunk of code I added but I don’t know what I’m looking for and the ‘hints’ use the same exact syntax I used so I think there’s something I should have done differently between the first if statement (it’s a tie) and the code that comes after that. Any help appreciated so I can move forward. Thanks!

     if (userChoice === computerChoice) {
       return ‘The game is a tie!’;
     }
    if (userChoice === ‘rock’) {
     if (computerChoice === ‘paper’) {
       return ‘The computer won!’;
     } else {
     return ‘You won!’;
     }
   }```

Hey there, what is the error message you are receiving? Javascript errors are usually very descriptive and give you both a line number and a reason, so it would be helpful to look at.

Hi! Yes, it says ‘syntax error: unexpected token )’ but I don’t see anything extra… and it doesn’t give a line number but it’s definitely in this block because I’ve been hitting ‘save’ after every block I add to make sure it’s error free.

Hmm that definitely implies there’s an extra closing bracket hidden somewhere in there. Would it be alright if you could post the full code and error message? Sometimes weird things can happen in scripts that can throw these off.

Absolutely, thanks for your help. Here’s all the code:

  userInput = userInput.toLowerCase();
  if (userInput === 'rock') {
  return userInput;
  } else if (userInput === 'paper') {
  return userInput;
  } else if (userInput === 'scissors') {
  return userInput;
  } else {
    return 'Invalid entry';
  }
};

const getComputerChoice = () => {
  let randomNumber = Math.floor(Math.random() * 3);
  if (randomNumber === 0) {
    return 'rock';
  } else if (randomNumber === 1) {
    return 'paper';
  } else if (randomNumber === 2) {
    return 'scissors';
  }
};

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'The game is a tie!';
  } 
  if (userChoice === 'rock') { 
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!';
    } 
  }

and the error message:
/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:36
});
^
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)

The only things I’m seeing in that code you sent is that some of the function definitions aren’t right. So determineWinner isn’t closed off and what I assume is the getUserChoice function doesn’t seem to have an initial part of the declaration. The error suggests that the issue is on line 36 which is a line I don’t seem to reach. Based on the error code it’s basically saying there’s an extra ) on line 36, but I can’t verify that myself.

Well there is no line 36 so I can’t show it to you. I’m not sure what the problem is that you see with how the functions are declared, like I said, I was testing it after adding each block and it was working until I added the last seven lines. I add ; at the very end, after the last curly brace and I still get the error. And overall, I’ve gone through and opened up all the ‘hints’ and my code matches the hints (and there was no ; after the last curly brace in the hint).

In the code you posted this is the first code chunk. However userInput is never actually taken in the code you sent me, plus there’s an extra curly bracket implying there’s missing parts of the declaration, does that make sense?

As for the semi-colon that won’t be the issue as those are more a best practise thing in Javascript than an actual requirement for running the code, plus they’re not generally put at the end of function declarations. The error definitely says to me that it’s an extra bracket that’s the issue, so I would check all your code to ensure that all the closing brackets have a matching opening one.

sorry, I didn’t notice that when i pasted the code it cut off the first line (both times). This is the full code:

`const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’) {
return userInput;
} else if (userInput === ‘paper’) {
return userInput;
} else if (userInput === ‘scissors’) {
return userInput;
} else {
return ‘Invalid entry’;
}
};

const getComputerChoice = () => {
let randomNumber = Math.floor(Math.random() * 3);
if (randomNumber === 0) {
return ‘rock’;
} else if (randomNumber === 1) {
return ‘paper’;
} else if (randomNumber === 2) {
return ‘scissors’;
}
};

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return ‘The game is a tie!’;
}
if (userChoice === ‘rock’) {
if (computerChoice === ‘paper’) {
return ‘The computer won!’;
} else {
return ‘You won!’;
}
};`

Ah okay thanks. So when I put that into my IDE, it flags that the determineWinner() function is never closed, perhaps that could be causing the issue? Add an extra closing curly bracket where the last semicolon is and that should hopefully help.

Ok, thanks so much, It was a curly bracket and I just found it. Bizarrely, when I tested that block it did work, but whatever. Appreciate your helping me out.

1 Like

Code can be fickle sometimes, one wrong tap and you can accidentally delete a bracket and break everything! I’m glad that worked.

1 Like

Oops, no. Forget that part about it working when I tested it, I got confused.