Skip to content

Instantly share code, notes, and snippets.

@oreillyross
Created May 15, 2016 22:03
Show Gist options
  • Save oreillyross/0afd7837097a8fe43219ae9e6169f2a9 to your computer and use it in GitHub Desktop.
Save oreillyross/0afd7837097a8fe43219ae9e6169f2a9 to your computer and use it in GitHub Desktop.
function checkCashRegister(price, cash, cid) {
var change = cash - price;
console.log('change: ' + change);
var cashInDrawer = function() {
var tot = 0;
cid.forEach(function(curr) {
tot = tot + curr[1]
// console.log(curr[1])
// console.log(tot)
})
return tot.toFixed(2);
};
var denomination = function(denom) {
var val;
switch (denom) {
case "PENNY":
val = .01
break;
case "NICKEL":
val = .5
break;
case "DIME":
val = .1
break;
case "QUARTER":
val = .25
break;
case "ONE":
val = 1
break;
case "FIVE":
val = 5
break;
case "TEN":
val = 10
break;
case "TWENTY":
val = 20
break;
case "HUNDRED":
val = 100
break;
}
return val;
}
var makeChange = function(change, denomination) {
return Math.abs(change - denomination);
}
//console.log(denomination('TWENTY'))
console.log('cashindrawer: ' + cashInDrawer());
if (change === 0) {
console.log('Closed');
return 'Closed'
} else if (change > cashInDrawer()) {
console.log('Insufficient Funds');
return 'Insufficient Funds'
} else {
// can get change
var changeArr = [];
cid.reverse();
cid.forEach(function(curr) {
if (change % denomination(curr[0]) === 0) {
console.log("Demonination: " + curr[0] + ' with ' + curr[1]);
console.log(makeChange(change, curr[1]));
if (makeChange(change, curr[1]) >= 0) {
changeArr.push(curr);
change = makeChange(change, curr[1]);
console.log('change is: ' + change)
}
}
})
console.log(changeArr);
}
//console.log(cashInDrawer())
}
// Example cash-in-drawer array:
// [["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]]
checkCashRegister(15.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]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment