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

107
Visualizações
Variable is being truncated and I'm not sure why

I have a bit of code that goes something like this:

function getTotal() {
    let total = 0;

    for (let i = 0; i < 5; i++) {
        let N = couldReturnANumberOrUndefined() | 0.00;
        total += N;
    }

    return total;
}

function couldReturnANumberOrUndefined() {
    const r = Math.floor(Math.random() * 2);
    if (r === 0) {
        return undefined;
    } else {
        return 1.5;
    }
}

The idea is that if the function returns undefined, we just say it was 0.00, hence the pipe.

The issue is that when it should return 1.5 - and I can see in the debugger that the return value is in fact 1.5, by the time it gets to be put into price it is always truncated down to the lowest integer value, and I have no idea why.

I've tried putting .toFixed(2) at the end of the price variable in various configurations but that hasn't worked.

Anyone know why this is and how to stop it?

Many thanks!

about 4 years ago · Juan Pablo Isaza
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