Skip to content

Instantly share code, notes, and snippets.

@eday69
Created June 16, 2018 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eday69/a71bd01c7144b29b0e106628621ce624 to your computer and use it in GitHub Desktop.
Save eday69/a71bd01c7144b29b0e106628621ce624 to your computer and use it in GitHub Desktop.
freeCodeCamp JavaScript Algorithms and Data Structures Projects: Cash Register
// Design a cash register drawer function checkCashRegister()
// that accepts purchase price as the first argument (price),
// payment as the second argument (cash), and cash-in-drawer
// (cid) as the third argument.
// cid is a 2D array listing available currency.
// The checkCashRegister() function should always return an object
// with a status key and a change key.
// Return {status: "INSUFFICIENT_FUNDS", change: []} if
// cash-in-drawer is less than the change due, or if you cannot
// return the exact change.
// Return {status: "CLOSED", change: [...]} with cash-in-drawer
// as the value for the key change if it is equal to the change due.
// Otherwise, return {status: "OPEN", change: [...]}, with the
// change due in coins and bills, sorted in highest to lowest order,
// as the value of the change key.
function checkCashRegister(price, cash, cid) {
// Here is your change, ma'am.
var amount = cash-price;
var cid2=cid.slice().reverse();
let arrChange=[];
let dividers={"PENNY": 0.01, "NICKEL": 0.05, "DIME": 0.1, "QUARTER": 0.25,
"ONE": 1, "FIVE": 5, "TEN": 10, "TWENTY": 20, "ONE HUNDRED": 100};
for (var currency of cid2) {
let coin_bill=dividers[currency[0]];
let qty=Math.min(Math.trunc(amount/coin_bill), currency[1]/coin_bill);
let curAmount=qty * coin_bill;
amount -= curAmount;
amount=amount.toFixed(2);
currency[1] -= curAmount;
arrChange.push([currency[0], curAmount]);
}
if (amount > 0) {
return {status: "INSUFFICIENT_FUNDS", change: []};
}
else if (cid2.reduce((acum, mySum) => {return acum+=mySum[1]},0) == 0) {
return {status: "CLOSED", change: arrChange.reverse()};
}
else {
return {status: "OPEN", change: arrChange.filter(item => item[1]>0)};
}
}
// Example cash-in-drawer array:
// [["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", 1.01], ["NICKEL", 2.05], ["DIME", 3.1],
["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60],
["ONE HUNDRED", 100]]); // 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]]); // {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]]); // {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]]); // {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]]); // {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]]); // {status: "CLOSED", change: [["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