Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function checkCashRegister(price, cash, cid) {
- var change = cash - price;
- let moneyList = [ [ 'ONE HUNDRED', 100 ],
- [ 'TWENTY', 20 ],
- [ 'TEN', 10 ],
- [ 'FIVE', 5 ],
- [ 'ONE', 1 ],
- [ 'QUARTER', 0.25 ],
- [ 'DIME', 0.1 ],
- [ 'NICKEL', 0.05 ],
- [ 'PENNY', 0.05 ] ]
- let cashInDrawer = cid.map(e=> e[1]).reduce((a,b)=> {return a + b},0)
- let arrChange = []
- if (change < cashInDrawer){
- for (let i=0;i < moneyList.length-1;i++){
- if (change > moneyList[i][1]){
- let a = change - change % moneyList[i][1]
- if (a > cid[cid.length-1-i][1]){
- a = cid[cid.length -1-i][1]
- }
- arrChange.push([moneyList[i][0],parseFloat(a.toFixed(4))] )
- change -= a
- }
- }
- if (arrChange.map(e=> e[1]).reduce((a,b)=>a + b,0) == 0){
- return {status: "INSUFFICIENT_FUNDS", change: []}
- }
- if (change > 0){
- arrChange.push([moneyList[8][0],parseFloat(change.toFixed(2))] )
- }
- return {status: "OPEN", change: arrChange};
- }
- if (change == cashInDrawer){
- return {status: "CLOSED", change: cid}
- }
- return {status: "INSUFFICIENT_FUNDS", change: []}
- }
- console.log(
- checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement