Skip to content

Instantly share code, notes, and snippets.

@crisberrios
Created March 24, 2015 21:13
Show Gist options
  • Save crisberrios/cf413ece442a38f14fa9 to your computer and use it in GitHub Desktop.
Save crisberrios/cf413ece442a38f14fa9 to your computer and use it in GitHub Desktop.
Cash Register Bonfire
function drawer(price, cash, cid) {
var values = {
'ONE HUNDRED': 100,
'TWENTY': 20,
'TEN': 10,
'FIVE': 5,
'ONE': 1,
'QUARTER': 0.25,
'DIME': 0.1,
'NICKEL': 0.05,
'PENNY': 0.01
};
function createDrawer(arr) {
return arr.
map(function(item) {
return newItem = [item[0], Math.round(item[1] / values[item[0]]),values[item[0]]];
}).
sort(function(a,b) {
return values[b[0]] - values[a[0]];
});
}
var avail = createDrawer(cid);
var pendingCash = cash-price;
var cashLeft = false;
avail = avail.
map(function(currency) {
console.log("pending:"+pendingCash);
var qty = Math.floor(pendingCash/currency[2]);
var amount = 0;
if (qty < currency[1]) {
amount = qty * currency[2];
cashLeft = true;
}
else if(qty > 0) {
amount = currency[1] * currency[2];
}
pendingCash = pendingCash.toFixed(2) - amount;
return [currency[0], amount];
}).
filter(function(i) {
return i[1] > 0;
});
console.log(avail);
console.log(cashLeft);
if(pendingCash > 0) {
return "Insufficient Funds";
}
else {
return cashLeft ? avail : "Closed";
}
}
drawer(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