Skip to content

Instantly share code, notes, and snippets.

@RobertSkalko
Last active August 11, 2016 21:10
Show Gist options
  • Save RobertSkalko/27e47be0ac4e60ed4daebd80b36e7667 to your computer and use it in GitHub Desktop.
Save RobertSkalko/27e47be0ac4e60ed4daebd80b36e7667 to your computer and use it in GitHub Desktop.
function checkCashRegister(price, cash, cid) {
var change=(cash-price).toFixed(2);
// 0:available, 1:money type, 2:name, 3:chang given
var money=[
[cid[0][1] ,0.01,'PENNY',0],
[cid[1][1] ,0.05,"NICKEL",0],
[cid[2][1] ,0.10,'DIME',0],
[cid[3][1] ,0.25,'QUARTER',0],
[cid[4][1],1,"ONE",0],
[cid[5][1],5,"FIVE",0],
[cid[6][1],10,"TEN",0],
[cid[7][1],20,"TWENTY",0],
[cid[8][1],100,"ONE HUNDRED",0]
];
var calc;
var arr=[];
var subarr=[];
for (var i=money.length-1;i>-1;i--){
if (money[i][0]>=money[i][1]){
while (change >= money[i][1] && money[i][0] >= money[i][1]){
change=(change-money[i][1]).toFixed(2);
money[i][3]=money[i][3]+money[i][1];
parseFloat(money[i][3]).toFixed(2);
money[i][0]=money[i][0]-money[i][1];
parseFloat(money[i][0]).toFixed(2);
}
}
}
var zero=0;
for (var x=money.length-1;x>-1;x--){
if (money[x][3]>0){
subarr.push(money[x][2]);
subarr.push(money[x][3]);
}
if (money[x][0]<0.01){
zero++;
}
if (subarr.length>0){
arr.push(subarr);
subarr=[];
}
}
if (change<0.02 && zero===money.length){return "Closed";}
if (change>0) { return "Insufficient Funds";}
return arr;
}
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]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment