I have the following code:
function checkCashRegister(price, cash, cid) {
// Move cid into an object
var cashReg = cid.reduce(function(prev, curr) {
prev[curr[0]] = curr[1];
return prev;
}, {});
// Total money in the register
var regSum = Object.values(cashReg).reduce(function(a, b) {
return a + b;
}, 0);
return regSum;
// --> 335,41
}
checkCashRegister(19.50, 20.00, [
["PENNY", 1.01],
["NICKEL", 2.05],
["DIME", 3.10],
["QUARTER", 4.25],
["ONE", 90.00],
["FIVE", 55.00],
["TEN", 20.00],
["TWENTY", 60.00],
["ONE HUNDRED", 100.00]
]);
The code itself seems to be working just fine, but for some reason when I return regSum the answer is 335,40999999999997 instead of 335,41. Why does this happen and how could I change my code so it would return the expected outcome?