Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda