Project Rock, Paper or Scissors - step 3

Link to the exercise

Hi everyone,

On step 3 of this project I tried defining the conditional statements with ternary operators, like so:
userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’ ? userInput : console.log(‘Error: not a possible choice.’);
They are within an arrow function, as suggested by the exercise.

It logs what you would expect in the case of false; but it does not log userInput in the case of true (just undefined).
When I write the normal if/else statement everything logs as it should.

Thanks for any feedback on this!

1 Like

Are you returning userInput for valid cases?


Hi Roy, I’m not sure I understand the question.

  1. The objective is indeed to log userInput (user input should be either ‘rock’, ‘paper’ or ‘scissors’) if true, and error message if false.
  2. I simply tried to practice using ternary operators instead of a normal if/else statement. The if/else statement valid cases correctly with no ‘undefined’ being logged, so I’m assuming I must be making some syntax mistake in the ternary syntax, but I can’t phantom what.
  3. After your question, I’ve also decided to include the keyword return after the question mark, like so ? : return userInput (but that gives an error).

Did I explain what you were asking me for in any of the 3 points above?

1 Like

When using a ternary, we can return the value that applies.

return condition(s) ? value : log(error_msg);

Roy, you’re a life saver. So that was my mistake. I was influenced by the syntax of the normal if/else, since the return is included in the codeblock and not at the beginning. Thank you so much. It all works now as it should. :slight_smile: