Rock paper scissors step 3

Functions | Codecademy

Why does not it return ‘rock’?

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') ? userInput
  : console.log("Type: rock, paper or scissors.");


There is no return statement in your function, hence, undefined is returned.

When using ternary I think I don’t need to use return after the :, and even using; it doesn’t work

Like if I change




it shows on console, but that’s not what I want

probably undefined is from the “if not true” part of the function, because it’s just executing a console.log and don’t return any value

Logging has no benefit to the program. You need to return, not log. That it is a ternary statement is of no significance. There is no implicit return (other than ‘undefined’) when curly braces are used.

    return (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') ? userInput : 'Type: rock, paper, or scissors.'

Thank you very much again, Roy!!! I was stuck in this exercise, but now I understand what you said. When testing, I was putting return after the condition, not before.

1 Like