My code always says it’s a “tie” whether it’s a tie or not. What am I doing wrong? Thanks.
// User input
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
return userInput;
} else {
console.log('Error!');
}
};
console.log(getUserChoice('rock'));
// Computer choice
const getComputerChoice = () => {
randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber){
case 0:
return 'rock';
break;
case 1:
return 'paper';
break;
case 2:
return 'scissors';
break;
default:
console.log('Error!');
break;
}
}
console.log(getComputerChoice());
// Determine winner
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());