Hey everyone!
Brand new to all of this, thank you for being there to help newbies like me out. So far I’m really enjoying it, and I’ve decided to start my coding journey with JavaScript.
I really enjoyed working through theRock, Paper, Scissors exercise, but I’m now getting a strange undefined error right under my result when I try to log the playGame function.
Based on my own investigating, Undefined seems to show up when there’s something in the code that’s not really needed. I tried isolating and logging each function to see which was generating the undefined, but everything seems to be fine right up until the very end when the playGame function is run, resulting in the undefined. Here’s my code:
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else {
console.log('Please enter a valid input');
}
}
function 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;
}
}
function determineWinner(userChoice, computerChoice) {
if (userChoice == computerChoice) {
return 'Tied game, play again!'
} else if (userChoice == 'rock' && computerChoice == 'paper') {
return 'The computer won, play again!'
} else if (userChoice == 'paper' && computerChoice == 'scissors') {
return 'The computer won, play again!'
} else if (userChoice == 'scissors' && computerChoice == 'rock') {
return 'The computer won, play again!'
} else {
return 'The user won, congrats!'
}
}
let userChoice = getUserChoice('RoCk');
let computerChoice = getComputerChoice();
function playGame(userChoice, computerChoice) {
console.log(`User played ${userChoice} and the computer played ${computerChoice}!`);
console.log(determineWinner(userChoice, computerChoice));
}
console.log(playGame(userChoice, computerChoice));
Thanks again!!