Hi,
I’m working on the rock, paper, scissors project:
Got to the end without any problems but when I call playGame() the determineWinner function expression returns ‘undefined’. I keep going over it and can’t see why.
My code is below:
const getUserChoice = userInput => {
userInput.toLowerCase();
if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
return 'You went for ' + userInput;
}else{
console.log('Please enter a valid option. You can use either rock, paper or scissors');
}
}
//console.log(getUserChoice('scissors'));
function getComputerChoice(){
let computerChoice = Math.floor(Math.random() * 3);
if(computerChoice === 1){
return 'The computer went for rock';
}else if(computerChoice ===2){
return 'The computer went for paper';
}else{
return 'The computer went for scissors';
}
}
//console.log(getComputerChoice());
function determineWinner(userChoice, computerChoice){
if(userChoice === computerChoice){
return 'It is a tie, try again';
}
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('paper', 'rock'));
const playGame = () =>{
const userChoice = getUserChoice('rock');
const computerChoice = getComputerChoice();
console.log(userChoice);
console.log(computerChoice);
console.log(determineWinner(userChoice, computerChoice));
};
playGame();