Codecademy Forums

Why does function result "undefined"?

Hi colleagues!
I have a function:

const getUserChoice = userInput => {
	userInput = userInput.toLowerCase();
 	if (userInput==='rock' || userInput==='paper' || userInput==='scissors' || userInput==='bomb') {
  	return userInput;
    } else {
  	return console.log('User input is wrong');
    }
};

console.log(getUserChoice('TEST'));

Function returns:

User input is wrong
undefined

Why did I get “undefined”? I didn’t log any empty variable! (only my text).

console.log appears twice in your code so you can’t really argue that you only use it once

Thank you!
As I understand I nested one “console.log” in one more “console.log” and as a result:
The first “console.log” prints “undefined”
The second prints my text.

Am i right?

You expected one output but called console.log twice. If you call it twice you’ll get two outputs. If you only expected one, delete one of them. Which one should be deleted? That depends on how you intend for your function to behave, and then you would adjust the rest to fit together with that.

Sure in some sense you’re nesting things but isn’t that a bit vague? You can fit things together however you want, but you the programmer are the one who has to make sure that they line up with each other in a useful way. Is it nested? In what way? One happens before the other, isn’t that sequential?

If you were to blame any one single thing then it would be this:

return console.log('User input is wrong');

What is the meaning of that return value? Which value are you using? Is your function supposed to both print and return something? Usually it’s one or the other, but either way there needs to be some intention behind it and this seems to lack some of that.

1 Like

It’s clear now. Thanks again)
This code was a part of the program “Rock, Paper, or Scissors” (Web development course).

I refactored code and it works correct.
There is a result:

const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
 		if (userInput==='rock' || userInput==='paper' || userInput==='scissors' || userInput==='bomb') {
  	return userInput;
  } else {
  	return 'User input is wrong'; 
}
};

const getComputerChoice = () => {
	choice = Math.floor(Math.random() * 3);
  switch (choice) {
    case 0:
    return 'rock';
    break;
    case 1:
    return 'paper';
    break;
    case 2:
    return 'scissors';
    break;
  }
};

let determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'Game is tie';
  } else if (userChoice === 'paper' && computerChoice === 'rock') {
    return 'User is the winner!';
  }  else if (userChoice === 'scissors' && computerChoice === 'paper') {
    return 'User is the winner!';
  } else if (userChoice === 'rock' && computerChoice === 'scissors') {
    return 'User is the winner!';
	} else if (userChoice === 'bomb') {
    return 'User is the winner!';
  } else {
		return 'User have lost!';
  }
}

let playGame = (userChoice) => {
  userChoice  = getUserChoice(userChoice); 
  
  if (userChoice !== 'User input is wrong') {
  computerChoice = getComputerChoice();
	console.log(userChoice + ' ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
  } else {
    return console.log(userChoice);
  }
}

playGame('ROCK');