Hi, I’m currently on the cash register challenge and ran into a weird occurrence using a for…in loop on the default array.
When using the loop to add every item of the array to find the total cash in drawer it gives an odd long decimal result when none of the input values got beyond the thousandth. All values added together equal to 335.41 but i’m getting 335.40999999999997. could someone explain what’s going on.
Thanks for any help.
function checkCashRegister(price, cash, cid) {
let cashInDrawer = 0;
for (let denom in cid) {
cashInDrawer+= cid[denom][1];
}
console.log(cashInDrawer)
}
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]]);
335.40999999999997
edit : I’m getting the same result for other loops too
for(let i = 0; i < cid.length; i++) {
cashInDrawer+= cid[i][1];
}
cid.forEach(denom => {cashInDrawer += denom[1]});