Hey Guys!
I have been having an undefined error show up (in relation to the Rock, Paper, Scissors JavaScript project) after I save my code after completing 13/14 tasks. I’ve been reading the code over again for the past few hours but no luck so far. This project has been tough but a good learning experience. I am also new to coding and JavaScript so still trying to learn how it operates. So, I was wondering if anyone else could help me figure out what the issues are. Thank you in advance!
The ‘undefined’ error:
2
You threw: scissors
The computer threw: undefined ← Error here
You won!
My 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 = (computerInput) => {
const randomNumber = console.log(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 “Tie game!”;
}
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!”;
}
}
};
const playGame = () => {
let userChoice = getUserChoice(‘scissors’);
let computerChoice = getComputerChoice();
console.log('You threw: ’ + userChoice);
console.log('The computer threw: ’ + computerChoice);
console.log(determineWinner(userChoice,computerChoice));
}
playGame();