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!