Hi , Thanks for reading my questions.
I try to work out Exact Change and all examples pass/work except one example .
Pls check as below.
function checkCashRegister(price, cash, cid) {
var change = [];
var length = cid.length;
// Here is your change, ma'am.
var sum = 0;
var funds = getFunds();
var minus = cash - price;
var units = {"ONE HUNDRED":100.00 , "TWENTY": 20.00,"TEN":10.00,
"FIVE": 5.00,"ONE":1.00,"QUARTER": 0.25,"DIME": 0.10,"NICKEL": 0.05,
"PENNY": 0.01};
function getFunds(){
for(var i=0;i<length;i++){
sum += cid[i][1];
}
return sum;
}
function countChange(index){
var currentUnit = units[cid[index][0]];
var count = cid[index][1];
while(minus>=currentUnit && count>=currentUnit){
minus-= currentUnit;count-= currentUnit;
}
if(cid[index][1]!==count){change.push([cid[index][0],cid[index][1]-count]);}
}
if(minus===0 || minus===0.50&&funds===0.50){return "Closed";}
if(minus>funds){return "Insufficient Funds";}
else{
for(var i = length-1;i>=0;i--){
countChange(i);
if(minus===0){return change;}
}
if(minus>0){return "Insufficient Funds";}
}
}
checkCashRegister(3.26, 100.00, [["PENNY", 1.01], ["NICKEL", 2.05],
["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00],
["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);
All other examples work/pass except below example:
checkCashRegister(3.26, 100.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);
Its result like ::
[["TWENTY", 60.00], ["TEN", 20.00], ["FIVE", 15.00], ["ONE", 1.00], ["QUARTER", 0.50], **["DIME", 0.20000000000018], ["PENNY", 0.03000000000027]]**
Really dont know why the last two elements will perform like as above…
Pls share your opinion on my work-out and point out the root cause.