Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Rock Paper Scissors and the !== comparison

I wanted to try using the !== conditional. Why does this code log 'Invalid choice' and undefined?

const getUserChoice = userInput => {
    userInput = userInput.toLowerCase();
    if (userInput !== 'rock' || userInput !== 'paper' || userInput !== 'scissors') {
        console.log('Invalid choice');
    } else {
        return userInput;
    }
};

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

I wanted to try the !== instead of === if statement for a rock paper scissors game. Is the issue syntax or logic?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This condition will always be true, regardless of the value of userInput:

userInput !== 'rock' || userInput !== 'paper' || userInput !== 'scissors'

You want to use && instead of ||, like this:

userInput !== 'rock' && userInput !== 'paper' && userInput !== 'scissors'

That way, you'll only see Invalid choice if userInput isn't one of the three allowed values.

Lastly, you're seeing undefined because you're logging the output of getUserChoice, but getUserChoice doesn't return anything when the if statement is hit.

about 4 years ago · Juan Pablo Isaza Report

0

You are getting Invalid choice because of Short-circuit evaluation. This means, the logical OR (||) expression is evaluated left to right and any of the left values get truthy-value then the rest will not be evaluated.

userInput !== 'rock' || userInput !== 'paper' || userInput !== 'scissors'

When userInput is equal to paper and execute the above condition The first one gets truthy-vaule because paper and rock are not the same. So that, the rest of them will not be evaluated (userInput !== 'paper' || userInput !== 'scissors'). That conditon become true and logged invalid choice. And in the condition you didn't return anything so getUserChoice function return undefined.

about 4 years ago · Juan Pablo Isaza Report

0

Your function is not returning anything on the truthy if path, only on the else path.

Also the inverse of if(x==="a" || x==="b" || ...) (aka if(!(...))) is if(x!=="a" && x!=="b" && ...) you need to swap || to && and && to || too, not just === to !== and !== to ===.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!