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

119
Views
Clean way to set numerical value by comparison conditional

I have the following const "maxRange" ... that compares two values and then sets to one of those values if less than. Is there a better/cleaner way to achieve this same logic?

const maxRange = Math.round(averageTotalPrice / 50) * 50 * 5.5 < maxTotalPrice ? Math.round(averageTotalPrice / 50) * 50 * 5.5 : maxTotalPrice
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Looks like you need Math.min:

const maxRange = Math.min(
    Math.round(averageTotalPrice / 50) * 50 * 5.5,
    maxTotalPrice
);
about 4 years ago · Juan Pablo Isaza Report

0

I think this better

let round = Math.round(averageTotalPrice / 50) * 50 * 5.5;
const maxRange = Math.min(round, maxTotalPrice);
about 4 years ago · Juan Pablo Isaza Report

0

I would start by getting rid of the magical numbers 50 and 5.5 and using Math.min instead of the ternary operator.

If Math.round(averageTotalPrice / MAGICAL_50) * MAGICAL_50 * MAGICAL_5_5 is not absolutely obvious in the domain you are working on, I would also put that in a constant just to make clear what that is all about.

Here is a hypothetical:

const BUCKET_SIZE = 5.5;
const WHOLESALE_AMOUNT = 50;
const maxRange = Math.min(Math.round(averageTotalPrice / WHOLESALE_AMOUNT ) * WHOLESALE_AMOUNT * BUCKET_SIZE, maxTotalPrice);

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!