Tell us what’s happening:
Dear Campers,
I have started the Cash Register Challenge and have run into a strange behaviour.
When I sum all the cash in the register cid
using .reduce
, I get a strange rounding error.
Instead of $335.41 I get $335.40999999999997.
While this behaviour is quite amusing, it also tells me that there are processes “under the hood” that I am not aware of…
So does anyone know why this is happening?
What pitfalls can I avoid in the future when implementing .reduce
?
**Your code so far**
function checkCashRegister(price, cash, cid) {
let changeDue = cash - price;
let sumInDrawer = cid.reduce((sum, a) => sum + a[1], 0);
console.log(changeDue, sumInDrawer);
return change;
}
checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36 Edg/89.0.774.45
.
Challenge: Cash Register
Link to the challenge: