Hi everyone,
working on cash register project, I encountered the following quirk: when I sum all the values in the new array I created with the numbers in each element of the cid array, I get a weird result:
function checkCashRegister(price, cash, cid) {
var change;
var arrSum = [];
cid.forEach(function (element) {
arrSum.push(element[1]);
});
console.log(arrSum); **// [1.01, 2.05, 3.1, 4.25, 90, 55, 20, 60, 100]**
var newArrSum = arrSum.reduce(function (acc, val) {
return acc + val;
});
console.log(newArrSum); **// 335.40999999999997**
}
The result should be 335.41, but I’m getting only a few decimals short of that. Why does this happen and how can I fix it?
Thanks!!!