Guest User

Untitled

a guest
Nov 19th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. ASSIGNMENT
  2. _______________________________________________________________________________________________________________________________
  3.  
  4. Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price),
  5. payment as the second argument (cash), and cash-in-drawer (cid) as the third argument.
  6.  
  7. cid is a 2D array listing available currency.
  8.  
  9. The checkCashRegister() function should always return an object with a status key and a change key.
  10.  
  11. Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due,
  12. or if you cannot return the exact change.
  13.  
  14. Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.
  15.  
  16. Otherwise, return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in highest to lowest order,
  17. as the value of the change key.
  18. _______________________________________________________________________________________________________________________________
  19.  
  20.  
  21. SOLUTION
  22.  
  23. _______________________________________________________________________________________________________________________________
  24.  
  25. function checkCashRegister(price, cash, cid) {
  26. var result = {status: "", change: []};
  27. // // Here is your change, ma'am.
  28.  
  29. var change = cash - price;
  30.  
  31. var cashValue = [
  32. { name: "ONE HUNDRED", value: 100 },
  33. { name: "TWENTY", value: 20 },
  34. { name: "TEN", value: 10 },
  35. { name: "FIVE", value: 5 },
  36. { name: "ONE", value: 1 },
  37. { name: "QUARTER", value: 0.25 },
  38. { name: "DIME", value: 0.1 },
  39. { name: "NICKEL", value: 0.05 },
  40. { name: "PENNY", value: 0.01 }
  41. ];
  42.  
  43. var cashInDrowerSum = cid.reduce(function(acc, item) { // get sum of all money we have in our drawer ('cid')
  44. return acc + item[1];
  45. }, 0);
  46.  
  47. if (cashInDrowerSum < change) {
  48. result.status = "INSUFFICIENT_FUNDS";
  49. return result;
  50. }
  51.  
  52. if (cashInDrowerSum === change) {
  53. result.status = "CLOSED";
  54. result.change = cid;
  55. return result;
  56. }
  57.  
  58. cid = cid.reverse(); // we are provided with an array ('cid'), where currency units of larger amount come last.
  59. // We need to reverse this array, so that they come in highest to lowest order
  60.  
  61. var reduced = cashValue.reduce(function(acc, next, index) { // we use optional parameter 'index' to get the index (position) of an
  62. // element from 'cashValue' array. Then we use this index to get access
  63. // to the correspondent element from the previously reversed 'cid' array
  64. // (e.g., first element from 'cashValue' array is an
  65. // object { name: "Hundred", value: 100 }. Its index is 0.
  66. // It corresponds to the first element from the 'cid' array,
  67. // ["ONE HUNDRED", 100]).
  68. var currentValue = 0;
  69. if (change >= next.value) {
  70. while (change >= next.value && cid[index][1] >= next.value) {
  71. currentValue += next.value; //add next.value-s, until it equals to 'change'
  72. change -= next.value; // decrement 'change' by the 'next.value' (which we added to the 'currentValue')
  73. change = Math.round(change * 100) / 100;
  74. cid[index][1] -= next.value; // take 'next.value' from our drawer
  75. }
  76. acc.push([next.name, currentValue]); // we provided 'initial value' (see line 83) and set it to an empty array,
  77. // so 'acc' is equal to an empty array.
  78. // That is, var reduced = [].push([next.name, currentValue])
  79. return acc;
  80. } else {
  81. return acc;
  82. }
  83. }, []); // we need to provide 2-dimentional array to pass the test,
  84. // so we need to specify an empty array, as our initial value
  85.  
  86. if (reduced.length < 1 || change > 0) { // if there are no elements in our 'reduced' array,
  87. // or if we still owe a change...
  88. // for example, if we have to give 50 cents,
  89. // but have only one dollar bill and one penny in our cash in drawer
  90. // ( checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0],
  91. // ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0],
  92. // ["TWENTY", 0], ["ONE HUNDRED", 0]]) )
  93. result.status = "INSUFFICIENT_FUNDS";
  94. return result;
  95. }
  96. result.status = "OPEN";
  97. result.change = reduced;
  98. return result;
  99. }
  100.  
  101. 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]])
Add Comment
Please, Sign In to add comment