Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Free Code Camp - Exact Change
var log;
var cashArr = [
["PENNY",0.01],
["NICKEL",0.05],
["DIME",0.10],
["QUARTER",0.25],
["ONE",1],
["FIVE",5],
["TEN",10],
["TWENTY",20],
["ONE HUNDRED",100]
];
function checkCashRegister(price, cash, cid) {
log = "";
var arr =[];
var change = cash-price;
cid = cid.reverse();
//log+=JSON.stringify(cid)+"\n\n";
//log+= "price:"+price+"\ncash:"+cash+"\nchange:"+change+"\n\n";
for(var i=0;i<cid.length;i++){
var $cid = cid[i][1];
var $cash = Cash(cashArr,cid[i][0]);
//log+= "$cid:"+cid[i][0]+"="+$cid +"\n$cash:"+$cash+"\n";
var c = Number.parseInt($cid/$cash);
var d = Number.parseInt(change/$cash);
if(d>c) d = c;
//log+= "c:"+c+"\nd:"+d+"\n";
if(change == 0) break;
if(d>0 && $cid>0 && d*$cash<=change){
var r = d*$cash;
cid[i][1] -= r;
change = (change - r).toFixed(2);
arr.push([cid[i][0],r]);
//log+="change:"+change+"\n";
//log+= r+"$$$\n";
}
//log+="\n";
//log+="|"+JSON.stringify(arr)+"|\n\n";
}
if(change == 0){
for(var a=0;a<cid.length;a++){
if(cid[a][1] > 0)
return JSON.stringify(arr);
}
return "Closed";
}
return "Insufficient Funds";
}
function Cash(arrCid,cashType){
for(var i=0;i<arrCid.length;i++){
if(arrCid[i][0] == cashType)
return arrCid[i][1];
}
}
checkCashRegister(19.50, 20.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]]);// should return an array.
//checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);// should return a string.
//checkCashRegister(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);// should return a string.
////checkCashRegister(19.50, 20.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]]);// should return [["QUARTER", 0.50]].
////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]]);// should return [["TWENTY", 60.00], ["TEN", 20.00], ["FIVE", 15.00], ["ONE", 1.00], ["QUARTER", 0.50], ["DIME", 0.20], ["PENNY", 0.04]].
//checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);// should return "Insufficient Funds".
//checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1.00], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);// should return "Insufficient Funds".
//checkCashRegister(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);// should return "Closed".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.