Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda