Advertisement
ErolKZ

Untitled

Sep 7th, 2021
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1.  
  2. function checkCashRegister(price, cash, cid) {
  3.  
  4.  
  5. let change = cash - price;
  6.  
  7. let status = '';
  8.  
  9. let arr = [];
  10.  
  11. let sumOfCash = 0;
  12.  
  13. let balance = 0;
  14.  
  15. let counter = 0;
  16.  
  17. let info = 0;
  18.  
  19. let arr2 = [];
  20.  
  21. let sumOfAllCash = 0;
  22.  
  23.  
  24.  
  25.  
  26.  
  27. let cashObject = {
  28.  
  29.  
  30. 'ONE HUNDRED': 100,
  31.  
  32. 'TWENTY': 20,
  33.  
  34. 'TEN': 10,
  35.  
  36. 'FIVE': 5,
  37.  
  38. 'ONE': 1,
  39.  
  40. 'QUARTER': 0.25,
  41.  
  42. 'DIME': 0.1,
  43.  
  44. 'NICKEL': 0.05,
  45.  
  46. 'PENNY': 0.01,
  47.  
  48.  
  49. }
  50.  
  51.  
  52.  
  53. for (let i = 0; i < cid.length; i++) {
  54.  
  55. sumOfAllCash += cid[i][1];
  56.  
  57. }
  58.  
  59.  
  60.  
  61.  
  62. for (let current in cashObject) {
  63.  
  64.  
  65. if (change - cashObject[current] >= 0) {
  66.  
  67.  
  68.  
  69.  
  70. for (let i = 0; i < cid.length; i++) {
  71.  
  72.  
  73. if (current === cid[i][0]) {
  74.  
  75. balance = cid[i][1];
  76.  
  77. }
  78.  
  79.  
  80. }
  81.  
  82.  
  83.  
  84. while (change - cashObject[current] >= 0 && balance > 0) {
  85.  
  86.  
  87.  
  88.  
  89. if (sumOfAllCash - change === 0) {
  90.  
  91. change = 0;
  92.  
  93. break;
  94.  
  95. } else if (sumOfAllCash - change < 0) {
  96.  
  97. info++;
  98.  
  99. break;
  100.  
  101. }
  102.  
  103.  
  104.  
  105. change -= cashObject[current];
  106.  
  107. balance -= cashObject[current];
  108.  
  109. sumOfCash += cashObject[current];
  110.  
  111. change = change.toFixed(2);
  112.  
  113. counter++;
  114.  
  115. }
  116.  
  117.  
  118. if (counter > 0) {
  119.  
  120. arr2.push(current, sumOfCash);
  121.  
  122. arr.push(arr2);
  123.  
  124. arr2 = [];
  125.  
  126. console.log(arr);
  127.  
  128. sumOfCash = 0;
  129.  
  130. balance = 0;
  131.  
  132. counter = 0;
  133.  
  134. }
  135.  
  136.  
  137.  
  138. }
  139.  
  140.  
  141.  
  142. }
  143.  
  144.  
  145.  
  146. console.log(change);
  147.  
  148.  
  149.  
  150. let changeObject = {
  151.  
  152. status: status,
  153.  
  154. change: arr,
  155.  
  156.  
  157. }
  158.  
  159.  
  160. if (change > 0) {
  161.  
  162. arr.length = 0;
  163.  
  164. }
  165.  
  166.  
  167.  
  168. if (arr.length > 0) {
  169.  
  170. changeObject.status = 'OPEN';
  171.  
  172. return changeObject;
  173.  
  174. } else if (info > 0 || change > 0) {
  175.  
  176. return { status: "INSUFFICIENT_FUNDS", change: [] };
  177.  
  178. } else if (arr.length === 0) {
  179.  
  180. changeObject.status = 'CLOSED';
  181.  
  182. changeObject.change = cid;
  183.  
  184. return changeObject;
  185.  
  186. }
  187.  
  188.  
  189.  
  190.  
  191.  
  192. return arr;
  193.  
  194. }
  195.  
  196.  
  197.  
  198. console.log(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement