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

64
Vistas
How to make a function that return an index with a limit

I've wondered how can I optimize the conditions to function that is flexible enough to return an index that has a limitation of 3 index

if(val === 0) return 0;
if (val === -90) return 3;
if (val === -180) return 2;
if (val === -270) return 1;
if (val === -360) return 0;
if (val === -450) return 3;

if(val === 0) return 0;
if (val === 90) return 1;
if (val === 180) return 2;
if (val === 270) return 3;
if (val === 360) return 0;
if (val === 450) return 1;

and so on

I wonder how to make a function that if a user increments by 90 the returns should get incremented with a limit of 3 then after 3 it will return 0 index

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

function f(x) {
 let z = x / 90;
 let y = z % 4;
 return y < 0 ? 4 + y : y;   
}



for(let i =  -90 * 10; i <= 90 * 10; i = i + 90) {
 console.log(`x = ${i} => result => ${f(i)}`);
}

function f(x) {
  return ((x / 90) % 4 + 4) % 4;
}


 for(let i =  -90 * 10; i <= 90 * 10; i = i + 90) {
  console.log(`x = ${i} => result => ${f(i)}`);
 }

about 4 years ago · Juan Pablo Isaza Denunciar

0

you should get the minimum input first, then add 360 to it.

val = 360+val;

if(val % 360 === 90){
    return 1
}
if(val % 360 === 180){
    return 2
}
if(val % 360 === 270){
    return 3
}
if(val % 360 === 0){
    return 0
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can try dividing by 90 for the positive numbers and for the negative numbers add 360 first then divide by 90.

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