Skip to content

Instantly share code, notes, and snippets.

@habibun
Created November 5, 2016 18:18
Show Gist options
  • Save habibun/8647f464bb9aa1cd8e876a0c6878b547 to your computer and use it in GitHub Desktop.
Save habibun/8647f464bb9aa1cd8e876a0c6878b547 to your computer and use it in GitHub Desktop.
Exact Change
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exact Change</title>
<script>
// Create an object which hold the denominations and their values
var denominations = [
{name: 'ONE HUNDRED', val: 100.00},
{name: 'TWENTY', val: 20.00},
{name: 'TEN', val: 10.00},
{name: 'FIVE', val: 5.00},
{name: 'ONE', val: 1.00},
{name: 'QUARTER', val: 0.25},
{name: 'DIME', val: 0.10},
{name: 'NICKEL', val: 0.05},
{name: 'PENNY', val: 0.01}
];
function checkCashRegister(price, cash, cid) {
var change = cash - price;
var totalCid = cid.reduce(function (acc, next) {
return acc + next[1];
}, 0.0);
if (totalCid < change) {
return "Insufficient Funds";
} else if (totalCid === change) {
return "Closed";
}
cid = cid.reverse();
var result = denominations.reduce(function (acc, next, index) {
if (change >= next.val) {
var currentValue = 0.0;
while (change >= next.val && cid[index][1] >= next.val) {
currentValue += next.val;
change -= next.val;
change = Math.round(change * 100) / 100;
cid[index][1] -= next.val;
}
acc.push([next.name, currentValue]);
return acc;
} else {
return acc;
}
}, []);
// console.log(result);
return result.length > 0 && change === 0 ? result : "Insufficient Funds";
}
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]]);
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment