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

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

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 Denunciar

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 Denunciar

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 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