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

134
Visualizações
Refactoring condition more efficient

I wrote a code with condition and for me I have lots a repetition of similar code. Can I make this code more efficient with a refactor ? After some research, that's the best that I can do that's just a part of the code, not the entire https://codepen.io/manofthelake/pen/GRyMzgW?editors=1010

  const firstNumber = 50
  const lastNumber = 1000
  const defaultMinNumber = firstNumber + Math.round((lastNumber - firstNumber) / 2 )
                                                       
  const selection = {
    min: null
    max: null
    }                                                     

  if (Object.values(selection).every(s => s === null)) {
    selection.min = defaultMinNumber
    selection.max = lastNumber
  }
  else if (selection.min < firstNumber) {
    savedSelection = {...selection}
    selection.min = (selection.max > firstNumber) ? firstNumber: defaultMinNumber
    selection.max = (selection.max > firstNumber) ? selection.max : lastNumber
  }
  else if (Object.values(savedSelection).every(s => s !== null)) {
    selection.min = (savedSelection.max > firstNumber) ? selection.min : defaultMinNumber
    selection.max = (savedSelection.max > firstNumber) ? selection.max : lastNumber
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Observations/Refactoring :

  • What is the different between lastNumber and defaultMinNumber as both will return same value. Looks like something has been missed in that calculation.

  • Instead of loop on each and every element you can use Nullish coalescing operator (??) which returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.

    selection.min ?? defaultMinNumber
    selection.max ?? lastNumber
    
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