Rock, Paper, Scissors Project Help

Hey Guys!

I have been having an error show up (in relation to the Rock, Paper, Scissors JavaScript Preformatted textproject) after I save my code. I’ve been trying to make changes and read the code over again for the past 3 hours but no luck so far. I am also new to coding and JavaScript so still trying to learn how it operates. So, I was wondering if anyone else could help me figure out what the issues are. Thank you in advance!

The error:
/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:31
if (userChoice === “rock”) {
^

ReferenceError: userChoice is not defined
at Object. (/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:31: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

My code:
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (
userInput === “rock” ||
userInput === “paper” ||
userInput === “scissors”
) {
return userInput;
} else {
console.log(“Error please, type: rock, paper, or scissors”);
}
};
const getComputerChoice = (computerInput) => {
const randomNumber = console.log(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 “Tie game!”;
}
};

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(“rock”, “scissors”));
console.log(determineWinner(“paper”, “scissors”));
console.log(determineWinner(“rock”, “rock”));

Remove the closing curly brace from the red line and move the closing brace to the green arrow (just before the console statements).

In the code posted by you, the closing curly brace (red line) signals the end of the determineWinner function’s body. userChoice is a parameter/variable in the scope of the determineWinner function. The ReferenceError alerts you that the expression (userChoice === "rock") can’t be evaluated since there is no userChoice variable in the global scope,

1 Like

Wow! It worked! Thank you very much!

1 Like