Skip to content

Instantly share code, notes, and snippets.

@rfprod
Last active April 22, 2017 15:52
Show Gist options
  • Save rfprod/de6e9ad3bc6faced1d53 to your computer and use it in GitHub Desktop.
Save rfprod/de6e9ad3bc6faced1d53 to your computer and use it in GitHub Desktop.
Exact Change
function exactChange(price, cash, cid) {
var change = cash - price;
var chArr = [];
var nominalNames = [];
//var nominalNames = ["PENNY", "NICKEL", "DIME", "QUARTER", "ONE", "FIVE", "TEN", "TWENTY", "ONE HUNDRED"];
// it is assumed that base nominal values for different currencies are standardised
var nominals = [0.01,0.05,0.1,0.25,1,5,10,20,100];
var drawerTotal = 0;
var changeDetailed = [];
var payoutStartNominalIndex;
// calculate drawer total and parse nominal names (nominal names can be defined statically also)
for (var f=0;f<cid.length;f++){
drawerTotal += cid[f][1];
nominalNames.push(cid[f][0]);
}
// get details on change to nominals ratio
for (var i=nominals.length-1;i>=0;i--){
changeDetailed.unshift(Math.floor(change / nominals[i]));
}
// detect min natural number !0 to start payout with
for (i=changeDetailed.length-1;i>=0;i--){
if (changeDetailed[i] >= 1){
payoutStartNominalIndex = i;
break;
}
}
// check what nominal to use for payout, form response array
var changedAmount = 0;
var rest = change;
for (i=payoutStartNominalIndex;i>=0;i--){
var dif = nominals[i] * changeDetailed[i];
if (cid[i][1] < dif){
if (rest - cid[i][1] > 0){
dif = cid[i][1];
}else{
var sum = 0;
for (var k=0;(nominals[i] * k) <= Math.ceil(rest*100)*0.01;k++){
sum = nominals[i] * k;
}
dif = sum;
console.log("sum "+i+": "+sum);
}
console.log("dif: "+dif);
rest -= dif;
console.log("rest: "+rest);
if (dif !== 0){
if (i !== 0){
chArr.push([nominalNames[i],dif]);
}else{
chArr.push([nominalNames[i],dif]);
}
}
}else{
console.log("dif: "+dif);
rest -= dif;
console.log("rest: "+rest);
chArr.push([nominalNames[i],dif]);
}
if (rest === 0){break;}
}
// check what to output array or string
var calculatedChange = 0;
for (var t=0;t<chArr.length;t++){
calculatedChange += chArr[t][1];
}
if (chArr.length < 1 || calculatedChange < change){
return "Insufficient Funds";
}else if (drawerTotal == change){
return "Closed";
}else{
return chArr;
//return nominals;
//return changeDetailed;
//return payoutStartNominalIndex;
}
}
//exactChange(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]]);
//exactChange(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]]);
//exactChange(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]]);
//exactChange(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
exactChange(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]]);

Exact Change

A cash register drawer function that accepts purchase price as the first argument, payment as the second argument, and cash-in-drawer (cid) as the third argument. cid is a 2d array listing available currency. Function returns the string "Insufficient Funds" if cash-in-drawer is less than the change due, the string "Closed" if cash-in-drawer is equal to the change due. Otherwise, it returns change in coin and bills, sorted in highest to lowest order.

A script by V.

License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment