Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!