Hey everyone, I’m stuck with a bug on the “Rock, Paper, Scissors”-project and can’t figure out what I did wrong.
The code works and executes until the “determineWinner()” function. It only executes the first if-statement and always returns “the game was a tie!” no matter what comes out of userChoice and computerChoice. If I remove the tie-if the rest of the code returns undefined.
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 = () => {
const randomNumber = Math.floor(Math.random()*3);
if (randomNumber === 0) {
let computerChoice = "rock";
return computerChoice;
}
if (randomNumber === 1) {
let computerChoice = "paper";
return computerChoice;
}
if (randomNumber === 2) {
let computerChoice = "scissors";
return computerChoice;
}
}
const determineWinner = (userChoice, computerChoice) =>
{
if (userChoice === computerChoice)
{
return "The game was a tie!";
}
if (userChoice === "rock")
{
if (computerChoice === "paper")
{
return "The computer won.";
} else
{
return "You won!";
}
};
if (userChoice === "paper")
{
if (computerChoice === "rock")
{
return "You win!";
} else {
return "Computer wins.";
}
};
if (userChoice === "scissors")
{
if (computerChoice === "rock") {
return "Computer wins.";
} else {
return "You win!";
}
}
};
function playGame() {
let userChoice = getUserChoice("rock");
let computerChoice = getComputerChoice();
console.log(userChoice);
console.log(computerChoice);
console.log(determineWinner());
}
playGame();