Rock, Paper, Scissors project

I got to the last step of the project ( # 13). When I call the function playGame, I get an error message stating that userChoice is undefined. Where did I go wrong, went back to make sure everything is spelled correctly. Below is my entire code ,Help!

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);
if (randomNumber === 0) {
return “rock”;
} else if (randomNumber === 1) {
return “paper”;
} else if (randomNcumber === 2) {
return “scissors”;
}
};

function determineWinner(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return “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!”;
}
}
};
function playGame() {
const userChoice = getUserChoice(“scissors”);
const computerChoice = getComputerChoice();
console.log(“You threw:” + userChoice);
console.log("Computer threw: " + computerChoice);
};

console.log(determineWinner(userChoice, computerChoice));
playGame();

Hello!

There is a error in Line 16 (Ncumber): } else if (randomNcumber === 2) {

Also, the last log in Line 55 should be inside the function playGame()

Try it :v:t2:

2 Likes

The variable userChoice only exists within the context of playGame function.

Since you placed console.log(determineWinner(userChoice, computerChoice)); outside of the playGame function (as @m.cassani mentioned) this will result in an undefined.

Happy coding!

2 Likes

OOOOOOHHHH! Thanks so much.