Help me find what causes undefined to show up in console. Rock, paper, scissors project

Why does my console show undefined after everything else in the code. The code works as it should, but it bugs me obviously to see that. I just cant find the reason it happens.

Heres my code:

// Function for the users input
function getUserChoice(userInput) {
    userInput = userInput.toLowerCase();
    if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') {
        return userInput;
    } else {
        console.log('Error!');
    }
}

// Function for randomly choosing the computers response
let getComputerChoice = () => {
    let randonNumber = Math.floor(Math.random() * 3);
    if ( randonNumber === 1) {
        return 'rock';
    } else if (randonNumber === 2) {
        return 'paper';
    } else {
        return 'scissors';
    } 
}

// Function to determine the winner using conditionals
let determineWinner = (getUserChoice, getComputerChoice) => {
    if (getUserChoice === getComputerChoice) {
        return 'Game was a tie';
    } if (getUserChoice === 'rock') {
        if (getComputerChoice === 'paper') {
            return 'Computer won';
        } else {
            return 'User won';
        }
    } if (getUserChoice === 'paper') {
        if (getComputerChoice === 'scissors') {
            return 'Computer won';
        } else {
            return 'User won';
        }
    } if (getUserChoice === 'scissors') {
        if (getComputerChoice === 'rock') {
            return 'Computer won';
        } else {
            return 'User won'
        }
    } if (getUserChoice === 'bomb') {
        if (getComputerChoice === 'rock' || getComputerChoice === 'paper' || getComputerChoice === 'scissors') {
            return 'User won! BOMB!!!'
        }
    }
}

// function to play the game. Input your choice inside the parameter of the function getUserChoice
function playgame () {
    let userChoice = getUserChoice('paper');
    let computerChoice = getComputerChoice();
    console.log('You threw: ' + userChoice);
    console.log('The computer threw: ' + computerChoice);
    console.log(determineWinner(userChoice, computerChoice));
}

// Logs the game to the console.
console.log(playgame());

Since you aren’t explicitly returning some value from the playgame function, so undefined is implicitly returned by the function which is then logged by the console.log statement,

// You wrote:
console.log(playgame());

// Change it to:
playgame();
1 Like

Ooh, i get it! Thanks for the help!

1 Like