Using if/else statement in Rock, Paper, Scissors challenge

Hi,

I’m currently working on the rock paper scissors excercise,

on Question 3 it says to great an if/else statement to check if the user inputs rock, paper, or scissors. I thought the below should work however this forms an error

const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput) === ('rock' || 'paper' || 'scissors') {
  return userInput;
} else {
  console.log ('There has been an error')
}
} ;

I see the correct way from the hint but I’m not sure why this would work and the way i did it wouldn’t. Any help would be great.

The Hint:

if (userInput === 'rock' || userInput === 'paper' || ... ) {
  return userInput;
} else {
  console.log('Error!');
}

Hello! This code

Checks to see if the string “rock” or “paper” or “scissors” are truthy values. Since they are strings, and are not empty (""), then they evaluate to a truthy value, which means that that statement (('rock' || 'paper' || 'scissors')) evaluates to true.

That means when you check userInput === ('rock' || 'paper' || 'scissors'), you are actually checking userInput === true, which returns false, as the === is a strict comparison operator, which checks type as well as value.

I hope this helps!

2 Likes