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

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