FCC JavaScript Algorithms and Data Structures Projects ( 5 ) :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 astatus
key and achange
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 keychange
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 thechange
key.
Currency Unit Amount Penny $0.01 (PENNY) Nickel $0.05 (NICKEL) Dime $0.1 (DIME) Quarter $0.25 (QUARTER) Dollar $1 (ONE) Five Dollars $5 (FIVE) Ten Dollars $10 (TEN) Twenty Dollars $20 (TWENTY) One-hundred Dollars $100 (ONE HUNDRED) Test Case:
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]]}
.
解決方案:
function checkCashRegister(price, cash, cid) {
//需要返回的物件
var output = { status: null, change: [] };
//錢幣面值,單位為美分
var denom = [
["ONE HUNDRED", 10000],
["TWENTY", 2000],
["TEN", 1000],
["FIVE", 500],
["ONE", 100],
["QUARTER", 25],
["DIME", 10],
["NICKEL", 5],
["PENNY", 1]
];
//收銀箱中的總額
var register = cid.reduce((acc, curr) => {
acc[curr[0]] = Math.round(curr[1] * 100);
acc.total += acc[curr[0]];
return acc;
}, { total: 0 });
//找零,單位美分
var change = Math.round((cash - price) * 100);
// 當找零剛好等於錢櫃中的總額
if (change == register.total) {
output.status = 'CLOSED';
output.change = cid;
return output;
}
//當找零大於錢櫃中的總額顯示金額不足
if (change > register.total) {
output.status = 'INSUFFICIENT_FUNDS';
return output;
}
//一般情況,收銀箱中的總額大於找零,但不一定就能找零,如果全是大面額的,就無法找零,只能返回金額不足
var arr = [];
for (let item of denom) {
var value = 0;
while (register[item[0]] > 0 && change >= item[1]) {
change -= item[1];
register[item[0]] -= item[1];
value += item[1];
}
if (value > 0) {
arr.push([item[0], value / 100]);
}
}
//雖然櫃子裡面的總額超過找零,但是沒有小額錢幣了,所以顯示金額不足
if (change > 0) {
output.status = 'INSUFFICIENT_FUNDS';
return output;
}
output.status = 'OPEN';
output.change = arr;
return output;
}