rock.Paper.Scissors.js

I’m getting the following error:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:29
if (userInput === ‘rock’){
^

ReferenceError: userInput is not defined
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:29:5)
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 pasted a copy of my code below. I can’t find my error. Please advise.

Thank you.

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’;
break;
case 1:
return ‘paper’;
break;
case 2:
return ‘scissors’;
break;
}
};
const determineWinner = (userInput, computerChoice)=>{
if (userInput === computerChoice){
return ‘The game is a tie!’;
}
}
if (userInput === ‘rock’){
if (computerChoice === ‘paper’){
return ‘The computer won!’;
} else {
return ‘You won!’;
}
}
if (userInput === ‘paper’){
if (computerChoice === ‘scissors’){
return ‘The computer won!’;
} else {
return ‘You won!’;
}
}
if (userInput === ‘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’));

Hi,

I believe you have misplaced an ‘}’ which is why the code does not work.
The error occurs in the ‘determineWinner’ function:

const determineWinner = (userInput, computerChoice)=>{
    if (userInput === computerChoice){
        return ‘The game is a tie!’;
    }
} // Remove this curly bracket.

if (userInput === ‘rock’){
    if (computerChoice === ‘paper’){
        return ‘The computer won!’;
    } else {
        return ‘You won!’;
    }
}

The function is closed before the conditionals for ‘rock’, ‘paper’ and ‘scissors’.
As userInput is a local variable, this causes a scope error.

Therefore, to solve the problem, you need to remove the ‘}’ in bold above (on line 29).
You also need to add another ‘}’ at the end of your function’s code (after ‘scissors’ if statement but before items are printed to the console.

I hope that helps

1 Like

Wow, thank you so much! That was the problem. I updated my code (see below) using your suggestion, and updated the userInput parameter to userChoice.

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’;
break;
case 1:
return ‘paper’;
break;
case 2:
return ‘scissors’;
break;
}
};
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’));

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.