Cash register help please!

Help! I’m super close to completing the cash register!
I’m having a precision issue with the larger set. Presumably in the calculations in the last while loop. Not sure where, how to address.

function checkCashRegister(price, cash, cid) {
 const denom =[
  ["PENNY", .01],["NICKEL", .05],["DIME", .1],["QUARTER", .25],["ONE", 1],
  ["FIVE", 5],["TEN", 10],["TWENTY", 20],["ONE HUNDRED", 100]
  ]
  var collector = []
 var drawer = Math.round(cid.reduce((a, b) => a + b[1], 0) *100)/100
var changeOwed = cash - price
var result = {status: '', change: []}
if (changeOwed > drawer){
  result.status = "INSUFFICIENT_FUNDS"
  return result 
}
if (changeOwed === drawer){
  result.status = "CLOSED"
  result.change = cid
return result
}
console.log(changeOwed)
     if (changeOwed > 0 && changeOwed <= drawer){   
    for (let i = denom.length-1; i >= 0; i--){
            if(denom[i][1] <= changeOwed){ 
                          var accum = []  
            
            while (denom[i][1] <= changeOwed && cid[i][1] > 0){
accum.push(denom[i][1])
changeOwed -= denom[i][1]
cid[i][1] -= denom[i][1]
          
          }
        collector.push([denom[i][0], accum.reduce((a, b) => a + b, 0)])
        }
    }
    }
console.log(collector)
console.log(changeOwed)
 if (changeOwed === 0){
result.status = "OPEN"
result.change = collector
console.log(JSON.stringify(result))
return result
}
    
    else {
result.status = "INSUFFICIENT_FUNDS"
return result
    }
}

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]]) ;

explain what you mean with “precision issue”, but just in case I’m linking a video

1 Like