“Rock, Paper, Scissors” Learn Javascript
Hey guys, I’m getting Reference error: userChoice is not defined. Can you please point out my mistake because this is my third time I have started this exercise from the scratch and just can’t figure it out.
Thank you!
function getUserChoice() {
var userInput = prompt("Rock, Paper or Scissors?");
userInput = userInput.toLowerCase();
if (userChoice === "rock" || userChoice === "paper" || userChoice === "scissors") {
return userInput;
} else {
console.log("Error!");
}
}
function getComputerChoice() {
Math.floor(Math.random() * 3);
switch(getComputerChoice) {
case 0:
return "rock";
case 1:
return "paper";
case 3:
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() {
var userChoice = getUserChoice();
var computerChoice = getComputerChoice();
console.log("You threw: " + userChoice);
console.log("The computer threw: " + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
playGame();