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

159
Visualizações
Is possible to make function from code and make it shorter?

I have this lines of code

let value = productDetails.recentPurchaseDate;
if (!productDetails.salesPrice && !productDetails.recentPurchaseDate) {
  value = false;
}
if (!productDetails.presentEstimatedValue) {
  value = true;
}

Is it possible to refactor, I need to make it function and this two IF? Thanks in advance

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Generally I don't think it is well designed if-conditions, but I don't know your business requirements. Let's try with something like this

let value = productHasRecentPurchaseDate(productDetails); //name should math your business goal

function productHasRecentPurchaseDate(productDetails) {
    if (!productDetails.salesPrice && !productDetails.recentPurchaseDate) {
       return false;
    }
    else if (!productDetails.presentEstimatedValue) {
       return true;
    }
    else {
        return /*you need all if statement path to return some value, this is last one, please provide what those statements should return if both previous conditions fail*/;
    }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can do it like this

function getValue({
    salesPrice,
    recentPurchaseDate,
    presentEstimatedValue
}) {
    if (!salesPrice && !recentPurchaseDate) return false;
    if (!presentEstimatedValue) return true
    return recentPurchaseDate
}

and then

let value = getValue(productDetails);
about 4 years ago · Juan Pablo Isaza Relatório

0

Something like that?

const value = yourNewFn();
function yourNewFn() {
    if (!productDetails.salesPrice && !productDetails.recentPurchaseDate) {
    value = false;
  }
  if (!productDetails.presentEstimatedValue) {
    value = true;
  }
  return value;
}

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