Rock, Paper, Scissors issue

Ok, this is driving me nuts. Where is my error in this code?

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);
switch (randomNumber) {
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 2:
return ‘scissors’;
}
};

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return “The game ends in a tie!”;
}
if (userChoice === ‘rock’) {
if (computerChoice === ‘paper’) {
return “Sorry, the computer won!”;
} else {
return “Congratulations, you won!”;
}
};

if (userChoice === ‘paper’) {
if (computerChoice === ‘scissors’) {
return “Sorry, the computer won!”;
} else {
return “Congratulations, you won!”;
}
};

if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return “Sorry, the computer won!”;
} else {
return “Congratulations, you won!”;
}
};

console.log(determineWinner(‘rock’, ‘scissors’));
console.log(determineWinner(‘rock’, ‘paper’));
console.log(determineWinner(‘scissors’, ‘paper’));
console.log(determineWinner(‘scissors’, ‘rock’));
console.log(determineWinner(‘paper’, ‘rock’));
console.log(determineWinner(‘paper’, ‘scissors’));

Everything seems to be fine except that I don’t really understand why you used case here. Try to replace the block of code starting with “const getComputerChoice = () => {…” with this and see if it works.

const getComputerChoice = () => {
    const randomNumber = Math.floor(Math.random() * 3)
    
};

I only used case because that´s what the guy was doing in the solution video. Still getting errors.

Here is my solution, and everything worked fine for me. Try it yourself and compare it with your code, it might help.

const getUsersChoice = 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) }; 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 === 'paper') { return 'You won!'; } else { return 'The computer won!' } } }; const playGame = () => { const userChoice = getUserChoice(); const computerChoice = getComputerChoice(); }; console.log(determineWinner('paper', 'scissors'));
1 Like