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

122
Visualizações
Simplifying IF/ELSE condition

I want to ask how can I simplify my code? It seems hard to read and has too much if-else condition here. Any way to simplify the code?

if (e.shiftKey && this.idx > 0) {
  this.idx= this.idx - 1;
} else if (!e.shiftKey && this.idx < trapFocus.length - 1) {
  this.idx = this.idx + 1;
} else if (!e.shiftKey && this.idx < trapFocus.length + 1) {
  this.idx= this.idx - 2;
} else if (e.shiftKey && this.idx > - 1) {
  this.idx= this.idx + 2;
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can simply separate the condition using separate if-else

if (e.shiftKey){
  if(this.idx > 0) this.idx = this.idx - 1;
  else if(this.idx > -1) this.idx = this.idx + 2;
} else {
  if(this.idx < trapFocus.length - 1)) this.idx = this.idx + 1;
  else if(this.idx < trapFocus.length + 1) this.idx < trapFocus.length + 1
}
about 4 years ago · Juan Pablo Isaza Relatório

0

This first thing you could do is to factor out e.shiftKey and use += and -= operators

if(e.shiftKey)
{
    if(this.idx > 0)
    {
        this.idx -= 1;
    }
    else if(this.idx > -1)
    {
        this.idx += 2;
    }
}
else{
    if(this.idx < trapFocus.length - 1)
    {
        this.idx += 1;
    }
    else if(this.idx < trapFocus.length + 1)
    {
        this.idx -= 2;
    }
}

If you ever want to go with ternaries:

this.idx += e.shiftKey ? (
        this.idx > 0 ? -1 :
        this.idx > -1 ? 2 : 0
    ) : (
        this.idx < trapFocus.length - 1 ? 1 :
        this.idx < trapFocus.length + 1 ? -2 : 0
    );

Note that this is not necessarily more readable, it just takes up less space.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can get it a bit more succinct by using the fact that your if clauses logically imply each other partly.

if e.shiftKey is true you change something if this.idx is 0 or more and if e.shiftKey is false you change something only if this.idx < trapFocus.length + 1:

let offset = 0;
if (e.shiftKey){
    if (this.idx >= 0) (this.idx ? offset = -1 : offset = 2)  
} else {
    if (this.idx < trapFocus.length + 1) 
       (this.idx < trapFocus.length - 1 ? offset = 1 : offset = -2)
}
this.idx += offset;

It is not necessarily much more readable.

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