https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-i/modules/fecp-learn-javascript-syntax-functions/projects/rock-paper-scissors-javascript
I cannot FIND this error? Appreciate any help!
/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:51
});
^
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)
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if(userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’) {
return userInput;
} else {
console.log(‘Error!’);
}
}
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 ‘The game is a tie!’;
}
if (userChoice === ‘rock’){
if (computerChoice === ‘paper’){
return ‘The computer won!’;
} else {
return ‘You won!’;
}
}
if (userChoice === ‘paper’){
if (computerChoice === ‘scissors’){
return ‘The computer won!’;
} else {
return ‘You won!’;
}
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return ‘The computer won!’;
} else {
return ‘You won!’;
}
}
console.log(determineWinner(‘paper’, ‘scissors’));
console.log(determineWinner(‘paper’, ‘paper’));
console.log(determineWinner(‘paper’, ‘rock’));
Hello, and welcome to the forums!
You haven’t ended your determineWinner()
function so it’s throwing it off.
You may have removed a }
because of the syntax highlighting after it looking like you made a mistake, but it’s an example of an arrow function making the syntax highlighting a little wonky in the learning environment sometimes. Add back the }
to end the function and you should be good.
2 Likes
Is there a good best-practice or tool devs use for “proofing” code? That error message wasn’t super helpful.
When you’re developing in environments that you have more control over, you can use tools like ESLint with your preferred editor that will give more “proofing” options. There’s also a section in the JavaScript course that includes more debugging topics to help you get used to the messages, along with techniques like commenting out blocks of code, adding logging, etc.
You’re right, sometimes the messages aren’t super helpful. In this case, I knew the statement it did complain about was fine, so I started looking at the lines before it
2 Likes
HI can you explain more about the missing }. I’ve been racking my brain about what this error meant before coming to the forums so I am really glad that I was not the only one that got this error. But I am a bit confused about what you mean about not ending my determineWInner( ) function. Here is my code below: can you show me where and explain??
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 ‘Congrats, you won!;’
}
}
if (userChoice === ‘paper’) { if (computerChoice === ‘scissors’)
{return ‘Sorry, computer won!’;
} else {
return ‘Congrats, you won!’;
}
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return ‘Sorry, computer won!’;
} else {
return ‘Congrats, you won!’;
}
};
console.log(determineWinner(‘rock’, ‘scissors’));
console.log(determineWinner(‘paper’, ‘scissors’));
console.log(determineWinner(‘rock’, ‘rock’));
Count how many {
you have in the determineWinner
function, and count how many }
you have. They should be the same.
1 Like
Thank you! so that worked out but then I encountered a new error code that I do not understand.
/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:27
if (userChoice === ‘rock’) { if (computerChoice === ‘paper’)
^
ReferenceError: userChoice is not defined
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:27:7)
at Module._compile (module.js:571:32)
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)
at bootstrap_node.js:542:3
I see that it has to do with my line 27 code but I don’t understand what is wrong. I first thought maybe I spelt something wrong but that wasn’t it. I then compared it to the help video and the code looks the same and then I looked at the control flow and looked maybe my earlier code was wrong but that seemed to work as well. So I am very lost. Can you please explain what this means? I noticed I am having a hard time understanding what some of the error messages mean.
The thing is you close your function before the if
:
function someFunc(param){
if (something === param){
//code
}
}
if (somethingElse === param) {
//code
}
This is essentially what has happened. Make sure the }
correspond to the correct {
.