I’m having a problem with an infinite loop. I think I found what is causing the problem, but I’m not sure how to fix this. Can someone explain why the change is returning 4.450000000000003? And also, can someone confirm that this is what is causing an infinite loop.
function checkCashRegister(price, cash, cid) {
let cashValues =[.01, .05, .1, .25, 1, 5, 10, 20, 100];
var eachUnit = [];
var change = [];
change = cash - price;
console.log(change)
while (change !== 0) {
var highestUnit = Math.max.apply(Math, cashValues.filter(function(x){return x <= change}
));
eachUnit.push(highestUnit);
console.log(highestUnit)
change = change - highestUnit;
}
return eachUnit;
}
console.log(checkCashRegister(95.50 , 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));
4.700000000000003
1
1
1
1
0.25
0.25
0.1
0.1
-Infinity
100
100