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

270
Visualizações
Operators in an if-else statement

I want to have a logic that returns a value based on 2 variables: a received value and a current value. I have simplified it from multiple else-if's to an operator.

The code is something like this:

var receivedState = 2;
var currentState = 1;
var allowedState = true;

if(receivedState == 2 && currentState !== 1) { 
  allowedState = 1;
} else if ((receivedState == 3 || 4 || 5) && (currentState !== 2)) { 
    allowedState = 2;
}

console.log(allowedState);

So what I am trying to do is if the received state is "2" AND current state is not "1" then I want to set the value of allowedState to 1.

What happens now is that it sets the value to 2.

For the other it does work. When the state is 3, 4 or 5 and the current state is not 2 then the value of allowedState is set to 2 except when the current state is something else than the value of 2, then the value of allowedState is 2.

How do I change my code so that the logic works as following:

If received value is 2 and current value is not 1 then return 1, else do nothing (return true). If received value is one of 3, 4 or 5 and current value is not 2 then return 2, else do nothing.

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
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