Could you help me understand why I got this "undefined"?


#1

Hi there,

I’m new here and started to learn JS through the “introduction to javascript” course.
I’m currently doing this part : rock paper scissors

I just finished it and checked the video to figure out if I did well and it seems that my code is working fine, and get the job done, but at the end , the console returns a line with “undefined” and I can’t figure out where it comes from.
Everything seems okay in the code, and the whole thing works fine so I don’t understand what’s happening.

I could go further to the next step since it works anyway but I want to understand what’s going on before moving on

Can you give me a hint ?

here is my code :

const getUserChoice = (userInput) => 
{
userInput = userInput.toLowerCase();
if (userInput === "rock" || userInput === "paper" || userInput === "scissors" || userInput === "bomb") {
return userInput}
else {console.log("This ain't good yo !")}
}

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 === "bomb") {console.log("User destroyed comp")}
	else if (userChoice === computerChoice) {console.log("This is a tie")}
	else if (userChoice === "rock")
  	{ 
    	if (computerChoice === "paper") {console.log('computer wins !')}
   		else {console.log("User wins !")}
  	}
  else if (userChoice === "paper")
    {
      if (computerChoice === "scissors") {console.log("computer wins!")}
      else {console.log("user wins !")}
    }
  else 
  	{
    	if (computerChoice === "rock") {console.log("computer wins !")}
      else {console.log("user wins !")}
  	}
}

const playGame = () =>
{
  const userChoice = getUserChoice("bomb");
  console.log("User plays " + userChoice);
  const computerChoice = getComputerChoice();
  console.log("computer plays " + computerChoice);
  
 console.log(determineWinner(userChoice,computerChoice));
}

playGame();

The console returns this :

User plays bomb
computer plays rock
User destroyed comp
undefined

what the undefined is about ? where it come from ?

Thanks a lot


#2

Anyone please ??

When I test it in the dev tools the console says that the line returning “undefined” is this one :

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

But I don’t understand cause everything is defined don’t you think ? I tried a few things and it seems that it’s the determineWinner who is the problem, but since the code is working it means that is defined no ? I would like to understand please… I checked the video but my code is the same, or I missing something…