Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. let denominations = [
  2. { name: 'ONE HUNDRED', value: 100.00 },
  3. { name: 'TWENTY', value: 20.00 },
  4. { name: 'TEN', value: 10.00 },
  5. { name: 'FIVE', value: 5.00 },
  6. { name: 'ONE', value: 1.00 },
  7. { name: 'QUARTER', value: 0.25 },
  8. { name: 'DIME', value: 0.10 },
  9. { name: 'NICKEL', value: 0.05 },
  10. { name: 'PENNY', value: 0.01}
  11. ];
  12.  
  13.  
  14. function checkCashRegister(price, cash, cid) {
  15. let change = cash - price;
  16. let totalCid = cid.reduce((acc, next) => {
  17. return acc + next[1];
  18. }, 0.0);
  19. if (totalCid < change) {
  20. return 'Insufficient Funds';
  21. } else if (totalCid === change) {
  22. return 'Closed';
  23. }
  24. cid = cid.reverse();
  25. let result = denominations.reduce((acc, next, index) => {
  26. if (change >= next.value) {
  27. let currentValue = 0.0;
  28. while (change >= next.value && cid[index][1] >= next.value) {
  29. currentValue += next.value;
  30. change -= next.value;
  31. change = Math.round(change * 100) / 100;
  32. cid[index][1] -= next.value;
  33. }
  34. acc.push([next.name, currentValue]);
  35. return acc;
  36. } else {
  37. return acc;
  38. }
  39. }, []);
  40. return result.length > 0 && change === 0 ? result : 'Insufficient Funds';
  41. }
  42.  
  43. // Example cash-in-drawer array:
  44. // [["PENNY", 1.01],
  45. // ["NICKEL", 2.05],
  46. // ["DIME", 3.10],
  47. // ["QUARTER", 4.25],
  48. // ["ONE", 90.00],
  49. // ["FIVE", 55.00],
  50. // ["TEN", 20.00],
  51. // ["TWENTY", 60.00],
  52. // ["ONE HUNDRED", 100.00]]
  53.  
  54. checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement