I made a Rock paper scissors game in simple JS and it keeps returning undefined at the end. It returns the userInput and even getComputerChoice() as a random number in a switch, but it keeps saying undefined at the end even though I have no undefined/empty return statements.
JS:
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock'){
return userInput;
} else if (userInput === 'paper'){
return userInput;
} else if (userInput === 'scissors'){
return userInput;
} else{
console.log('Error! not valid input.')
return;
}
}
const getComputerChoice = () => {
let 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 'Game was a tie';
} else if (userChoice === 'rock'){
if (computerChoice === 'paper'){
return 'Computer wins!';
} else{
return 'User wins!';
}
} else if (userChoice === 'paper'){
if (computerChoice === 'scissors'){
return 'Computer wins!';
} else{
return 'User wins!';
}
} else if (userChoice === 'scissors'){
if (computerChoice === 'rock'){
return 'Computer wins!';
} else{
return 'User wins!';
}
} else{
return 'idk!';
}
}
const playGame = () => {
const userChoice = getUserChoice('scissors');
const computerChoice = getComputerChoice();
console.log('You threw ' + userChoice + ' '+ 'AI threw ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
console.log(playGame());