WHY ISNT MY JS CODE WORKING || Rock paper scissors

WHEN I RUN THE CODE… INSTEAD OF Logging The Computer Wins! the computer logs ‘undefined’ WHY

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

function getComputerChoice() {

const NumberChoice = Math.floor(Math.random()*3)

if (NumberChoice === 0) {
return ‘rock’} else if (NumberChoice === 1) {
return ‘paper’} else {
return ‘scissors’
}
};

function determineWinner(userChoice, computerChoice) {

if (userChoice === computerChoice) {
return ‘The game is tie! No one wins!’

if (userChoice === ‘rock’ && computerChoice === ‘paper’) {

return ‘The Computer Wins!’
}

}}

console.log(determineWinner(‘rock’,‘paper’))

It loggs undefined, why? any help would be greatfull!

undefined can mean two things: you explicitly returned undefined, or nothing was returned.

In your case, its the latter. The reason for this is that this:

if (userChoice === ‘rock’ && computerChoice === ‘paper’) {

return ‘The Computer Wins!’
}

Is nested within the if clause which handles a tie. So even if this code would be reachable (which its not due to return the game is a tie), it could never evaluate to true.

This comparison should be outside/after determining the game is a tie

1 Like

THANKS VERY VERY MUCH! MY CODE FINALLY WORKS THANKS U stetim94!

1 Like