# Cash Register loss of precision

Tell us what’s happening:
the pennies element is supposed to be .04 but it returns .03. how do I compensate for the loss in precision?

Your code so far

``````
function checkCashRegister(price, cash, cid) {
var status = {
status:"",
change:[]

};
var currency = {
"ONE HUNDRED":100,
"TWENTY":20,
"TEN":10,
"FIVE":5,
"ONE":1,
"QUARTER":.25,
"DIME":.1,
"NICKEL":.05,
"PENNY":.01
}
var change=cash-price;
var times=0;
var label="";
var billNum =0;
var drawerAmnt=0;
var count=0;

// Here is your change, ma'am.
for(var i=cid.length-1;i>=0;i--){
drawerAmnt+=cid[i][1];
billNum = currency[cid[i][0]];
times=Math.floor(change/currency[cid[i][0]]);
label=cid[i][0];
if(billNum<=change||cid[i][1]==0){

for(var j=0;j<times;j++){
if(count<cid[i][1]){
count+=billNum;
}

}

status.change.unshift([label, Math.round(count * 100) / 100]);
if(cid[i][1]!=0){
drawerAmnt-=times*billNum;
}

change-=count;
count=0;
}

}
if(change>.1){
status.status = "INSUFFICIENT_FUNDS";
status.change = [];
}else if(drawerAmnt==0){
status.status="CLOSED";
}else{
status.status="OPEN";
}

if(status.status==""){

}

return status;
}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]

console.log(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));

``````

Your browser information:

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36`.

Always do the calculations in cents, not dollars.

1 Like

take a look at this video, it will explain the issue pretty well

2 Likes

thank you very much:)

thank you, I learned a lot form the video:)