I have a problem… When I run my code, computerChoice
is undefined. It’s not wrong, it is. I know that my random number should be the value of computerChoice, but how am I meant to implement it into my code? Instead of computerChoice shouldn’t I just put randomNumber
? e.g:
const determineWinner = (userChoice, randomNumber) => {
Here’s the full code:
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
return userInput
} else{
console.log('ERROR!')
}
};
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;
}
}
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'Tie!';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'The user won!';
}
}
if (userChoice === 'paper') {
if (computerChoice === 'rock') {
return 'The user won!';
} else {
return 'The computer won!'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'paper') {
return 'The user won!';
} else {
return 'The computer won!';
}
}
}
const playGame = () => {
console.log(determineWinner(2, computerChoice));
}
playGame();