Hi all
Tried working on the paper scissors rock game. I’ve tried to do this without using the hints, but I hit a bit of a roadblock. My code for the determineWinner
spits out the correct results when userChoice
= rock
. But when userChoice
= scissors or paper, the tie game is correct, but if not a tie, it always spits out that Player won
, even if the computer had a winning choice (ie userChoice
= paper
, computerChoice
= scissors
). Anyone know why the rock
option gives the correct results, but scissors
and paper
don’t? Hope that makes sense! Thank you in advance Code is below.
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === 'rock') {
return userInput
} if (userInput === 'paper') {
return userInput
} if (userInput === 'scissors') {
return userInput ;
} else {
return 'error'
}
};
//console.log(getUserChoice('rock'));
//console.log(getUserChoice('gun'));
const getComputerChoice = () => {
const randomNumber = 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 'Game is a tie'
} if (userChoice === 'rock' && computerChoice === 'paper') {
return 'Computer won'
} else {
return 'Player won'
} if (userChoice === 'paper' && computerChoice === 'scissors') {
return 'Computer won'
} else {
return 'Player won'
} if (userChoice === 'scissors' && computerChoice === 'rock') {
return 'Computer won'
} else {
return 'Player won'
}
};
console.log(determineWinner('rock', 'rock'));
console.log(determineWinner('rock', 'scissors'));
console.log(determineWinner('rock', 'paper'));
console.log(determineWinner('paper', 'paper'));
console.log(determineWinner('paper', 'rock'));
console.log(determineWinner('paper', 'scissors'));
console.log(determineWinner('scissors', 'scissors'));
console.log(determineWinner('scissors', 'paper'));
console.log(determineWinner('scissors', 'rock'));