Why isn't the function working as expected? I need the change to be 0. I believe there must be a more efficient way to solve this issue, but what's bothering me is that the "change" variable is giving strange values...need help!
function checkCashRegister(price, cash, cid) {
var change= cash-price
var money={
'PENNY':0.01,
'NICKEL':0.05,
'DIME':0.1,
'QUARTER':0.25,
'ONE':1,
'FIVE':5,
'TEN':10,
'TWENTY':20,
'ONE HUNDRED':100}
console.log(change,'start')
let i=8;
while(change>0){
// console.log(cid[i][1])
if(change<cid[i][1]&&change>money[cid[i][0]]){
console.log(cid[i][0], 'cid')
console.log(money[cid[i][0]],'money')
change=change-money[cid[i][0]]
console.log(change, 'change')
i=8;
}
i--
}
}
checkCashRegister(19.4, 20,
[["PENNY", 1.01],
["NICKEL", 2.05],
["DIME", 3.1],
["QUARTER", 4.25],
["ONE", 90],
["FIVE", 55],
["TEN", 20],
["TWENTY", 60],
["ONE HUNDRED", 100]]);