Skip to content

Instantly share code, notes, and snippets.

@loonylou
Last active June 12, 2018 01:21
Show Gist options
  • Save loonylou/27e26a71bf4ab2bc6490ca01764c83a2 to your computer and use it in GitHub Desktop.
Save loonylou/27e26a71bf4ab2bc6490ca01764c83a2 to your computer and use it in GitHub Desktop.
Cash Register - FCC JS Project
function checkCashRegister(price, cash, cid) {
let change = 0;
let cashInDrawer = getSumOfCID(cid); // total in drawer
let changeRequired = cash - price; // total to give back
// Can I make change?
if (changeRequired > cashInDrawer) {
change = {status: "INSUFFICIENT_FUNDS", change: []};
}
else if (changeRequired == cashInDrawer) {
change = {status: "CLOSED", change: cid};
}
else {
let changeMakeUp = getChangeMakeUp(changeRequired, cid);
if(changeMakeUp == 0) {
change = {status: "INSUFFICIENT_FUNDS", change: []};
} else {
change = {status: "OPEN", change: changeMakeUp};
}
}
//console.log(change);
return change;
}
// How much money is in the drawer
function getSumOfCID(cid) {
let sum = 0;
cid.forEach((entry) => {
sum += entry[1];
});
return sum.toFixed(2);
}
// What coins make up the change total to give
function getChangeMakeUp(changeRequired, cid) {
let denominations = [
['ONE HUNDRED', 100],
['TWENTY', 20],
['TEN', 10],
['FIVE', 5],
['ONE', 1],
['QUARTER', 0.25],
['DIME', 0.10],
['NICKEL', 0.05],
['PENNY', 0.01]
];
let cidReversed = cid.reverse();
let changeMoney = [];
let numberTaken = 0;
let i = 0;
cidReversed.forEach(function(cash) {
let currentDenomination = denominations[i][0];
let currentDenominationValue = denominations[i][1];
let currentDenominationBalance = cash[1];
if(changeRequired / currentDenominationValue > 1 && currentDenominationBalance > currentDenominationValue) {
// Max number of times I can take from denomination based on CID
let maxNoCurrentDenom = currentDenominationBalance / currentDenominationValue;
// How many of the denomination I need to make change
let noCurrentDenom = parseInt(changeRequired / currentDenominationValue);
// Don't take more than exists
(noCurrentDenom > maxNoCurrentDenom) ? numberTaken = maxNoCurrentDenom : numberTaken = noCurrentDenom;
// Calculate how much I have taken, what's left, how much more I need
let currentDenomInHand = currentDenominationValue * numberTaken;
changeRequired = (changeRequired - currentDenomInHand).toFixed(2);
currentDenominationBalance -= currentDenomInHand;
changeMoney.push([currentDenomination, currentDenomInHand]);
}
i++;
});
return changeMoney;
}
// TESTS
// checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])
checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])
// checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
// checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
// checkCashRegister(19.5, 20, [["PENNY", 0.5], ["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