hasanqqsp

hasan_cash-register

Nov 30th, 2021
867
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function checkCashRegister(price, cash, cid) {
  2.   var change = cash - price;
  3.   let moneyList = [ [ 'ONE HUNDRED', 100 ],
  4.                     [ 'TWENTY', 20 ],
  5.                     [ 'TEN', 10 ],
  6.                     [ 'FIVE', 5 ],
  7.                     [ 'ONE', 1 ],
  8.                     [ 'QUARTER', 0.25 ],
  9.                     [ 'DIME', 0.1 ],
  10.                     [ 'NICKEL', 0.05 ],
  11.                     [ 'PENNY', 0.05 ] ]
  12.   let cashInDrawer = cid.map(e=> e[1]).reduce((a,b)=> {return a + b},0)
  13.   let arrChange = []
  14.   if (change < cashInDrawer){
  15.     for (let i=0;i < moneyList.length-1;i++){
  16.       if (change > moneyList[i][1]){
  17.         let a = change - change % moneyList[i][1]
  18.         if (a > cid[cid.length-1-i][1]){
  19.           a = cid[cid.length -1-i][1]
  20.         }
  21.         arrChange.push([moneyList[i][0],parseFloat(a.toFixed(4))] )
  22.         change -= a
  23.       }
  24.     }
  25.       if (arrChange.map(e=> e[1]).reduce((a,b)=>a + b,0) == 0){
  26.       return {status: "INSUFFICIENT_FUNDS", change: []}
  27.       }
  28.  
  29.     if (change > 0){
  30.       arrChange.push([moneyList[8][0],parseFloat(change.toFixed(2))] )
  31.   }
  32.  
  33.       return {status: "OPEN", change: arrChange};
  34.     }
  35.  
  36.  
  37.   if (change == cashInDrawer){
  38.     return {status: "CLOSED", change: cid}
  39.   }
  40.  
  41.   return {status: "INSUFFICIENT_FUNDS", change: []}
  42. }
  43. console.log(
  44. checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));
RAW Paste Data