Skip to content

Instantly share code, notes, and snippets.

@Shaikot007
Last active June 11, 2024 06:16
Show Gist options
  • Save Shaikot007/ef5a4e2bf2d13b74dd0a3f4c38acb8a9 to your computer and use it in GitHub Desktop.
Save Shaikot007/ef5a4e2bf2d13b74dd0a3f4c38acb8a9 to your computer and use it in GitHub Desktop.
Cash register. (freeCodeCamp JavaScript algorithms and data structures challenge)
function checkCashRegister(price, cash, cid) {
var change = cash - price;
var coinValues = [
{ 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 }
];
var totalmoney = cid.reduce(function (x, y) {
return x + y[1];
}, 0.0);
if (totalmoney < change) {
return {status: "INSUFFICIENT_FUNDS", change: []};
};
if (totalmoney === change) {
return {status: "CLOSED", change: cid};
};
var reversecid = cid.slice().reverse();
var bills = coinValues.reduce(function (acc, next, index) {
if (change >= next.val) {
var coins = 0.0;
while (change >= next.val && reversecid[index][1] >= next.val) {
change -= next.val;
change = change.toFixed(2);
reversecid[index][1] -= next.val;
coins += next.val;
}
acc.push([next.name, coins]);
return acc;
}
else {
return acc;
}
}, []);
if (change % totalmoney !== 0) {
return {status: "INSUFFICIENT_FUNDS", change: []};
};
return {status: "OPEN", change: bills};
};
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]])
// should return an object.
// 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]])
// should return {status: "OPEN", change: [["QUARTER", 0.5]]}.
// 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]])
// should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}.
// checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
// should return {status: "INSUFFICIENT_FUNDS", change: []}.
// checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
// should return {status: "INSUFFICIENT_FUNDS", change: []}.
// checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
// should return {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}.
// freeCodeCamp javaScript algorithms and data structures challenge link is given below:
// https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment