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

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

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 Denunciar

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 Denunciar

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