Rock Paper Scissors project throwing a mysterious undefined

Can anyone see why I’m throwing undefined in the console here?

const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (
userInput === “rock” ||
userInput === “paper” ||
userInput === “scissors”
) {
return userInput;
} else {
console.log(“Error!”);
}
};

const getComputerChoice = () => {
const 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 “This game is a tie!”;
}
if (userChoice === “rock”) {
if (computerChoice === “scissors”) {
return “You won!”;
} else {
return “The computer won!”;
}
}

if (userChoice === “paper”) {
if (computerChoice === “rock”) {
return “You won!”;
} else {
return “The computer won!”;
}
}

if (userChoice === “scissors”) {
if (computerChoice === “paper”) {
return “You won!”;
} else {
return “The computer won!”;
}
}
};

const playGame = () => {
const userChoice = getUserChoice(“rock”);
const computerChoice = getComputerChoice(“”);
console.log(You threw: ${userChoice});
console.log("The computer threw: " + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
};

console.log(playGame());

That’s why.

Hint

What does your playGame() function return?
console.log() prints the evaluated arguments that we list between the (), so your function is executed, and the value returned by your function is printed to the console.

Additional Hint

Every function in JavaScript returns something. If the function doesn’t explicitly return a value, then it implicitly returns undefined.

You can simply call the function without passing it to console.log(), or have your function return what you want printed rather than printing it from within the function.

P.S. Welcome to the forums! Also please review, How do I format code in my posts?, before posting code in the future. Having your code formatted in the forums the same way it looks in your code editor makes it much easier for others to help you.

3 Likes