Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

58
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!