Rock, paper, scissors - undefined

Project link: Rock, Paper & scissors

Hi,
I am able to see my results and who would win the battle - but it also displays “undefined” (ex. User Choice: paper Computer Choice: rock User wins! undefined) and I am not sure why this is happening. Any help is appreciated!
code below:

User Choice: paper Computer Choice: rock User wins! undefined

// User Choice
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase(); // fix any issues due to case sensitive letters
if (userInput===‘rock’||userInput===‘paper’||userInput===‘scissors’ ||userInput === ‘bomb’) {
return userInput;
} else {
return ‘Please enter a valid input.’;
}
}

/*
// Test if its working
console.log(getUserChoice(‘rOck’))
console.log(getUserChoice(‘rck’))

*/

// Computer Choice

let getComputerChoice= () => {
compChoice=Math.floor((Math.random()*3)); //use 3, because math.floor is not inclusive!
switch (compChoice){
case 0:
return ‘rock’;
break;
case 1:
return ‘paper’;
break;
case 2:
return ‘scissors’;
break;
};
};

/*

// Test computer choice, it will automatically use
console.log(getComputerChoice());
*/

// I do not need to define userChoice and ComputerChoice as variables, when I call the function all I need to do is use getUserChoice and getComputerChoice to use my functions!

function determineWinner(userChoice, computerChoice) {
if (userChoice===‘bomb’) {
console.log(‘You the BOMB! Lucky Winner!’)
} else if (userChoice===computerChoice) {
console.log(‘It’s a tie’);
} else if (userChoice === ‘rock’ && computerChoice !==‘paper’){
console.log(‘User wins!’);
} else if (userChoice === ‘paper’ && computerChoice !==‘scissors’){
console.log(‘User wins!’);
} else if (userChoice === ‘scissors’ && computerChoice !== ‘rock’){
console.log(‘User wins!’);
} else {
console.log(‘computer wins!’);
}
}

/*
// Test function before using functions
determineWinner (‘paper’,‘rock’)

//this is not what I was supposed to do, but it is a viable option:
getUserChoice (‘rock’);
Console.log(determineWinner(getUserChoice,getComputerChoice));
*/

const playGame=() => {
const userChoice = getUserChoice(‘paper’); //we need to have a valid input here
const computerChoice = getComputerChoice();
console.log(User Choice: ${userChoice});
console.log(Computer Choice: ${computerChoice});

console.log(determineWinner(userChoice, computerChoice));
};

playGame();

Hello @py0544782574, welcome to the forums! The reason this is happening is that in the playGame function, you console.log() the determineWinner function:

However, the determineWinner function does not return any values-it prints them as well:

(notice the console.logs?)

When you print something to the console, the program cannot use that value-you can see it, but the program can’t. Therefore, since the function returns undefined, as it doesn’t actually return anything, that gets printed to the console, as well as everything inside the function.
I hope this helps!

2 Likes

Yes, I see it now. Thank you :smile:

2 Likes

If you print that within function that will print undefined along with the data that are supposed t print. So use return in function so that will only return the the data you want to display or print. I guess when we print using console.log() function that will also get the object.

1 Like

Hello @dipesh79, welcome to the forums!

In almost every case, yes!