Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda