Advertisement
ErolKZ

Untitled

Jul 14th, 2021
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. function solve(input) {
  2.  
  3.  
  4. let expectedSum = Number(input[0]);
  5.  
  6. let index = 1;
  7.  
  8. let card = 0;
  9.  
  10. let cash = 0;
  11.  
  12. let avgCard = 0;
  13.  
  14. let avgCash = 0;
  15.  
  16. let counter = 0;
  17.  
  18. let moneyCash = 0;
  19.  
  20. let moneyCard = 0;
  21.  
  22. let totalMoney = 0;
  23.  
  24. let moneyCash2 = 0;
  25.  
  26. let moneyCard2 = 0;
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. while (input[index] !== 'End') {
  36.  
  37. let current = Number(input[index]);
  38.  
  39. counter += 1;
  40.  
  41.  
  42.  
  43.  
  44. if (current <= 100 && counter % 2 !== 0) {
  45.  
  46. cash += 1;
  47.  
  48. moneyCash = current;
  49.  
  50. console.log(`Product sold!`);
  51.  
  52. } else if (current >= 10 && counter % 2 === 0) {
  53.  
  54. card += 1;
  55.  
  56. moneyCard = current;
  57.  
  58. console.log(`Product sold!`);
  59.  
  60. } else {
  61.  
  62. console.log(`Error in transaction!`);
  63.  
  64. }
  65.  
  66. totalMoney += moneyCard + moneyCash;
  67.  
  68. moneyCash2 += moneyCash;
  69.  
  70. moneyCard2 += moneyCard;
  71.  
  72. moneyCash = 0;
  73.  
  74. moneyCard = 0;
  75.  
  76.  
  77. if (totalMoney >= expectedSum) {
  78.  
  79. avgCash = moneyCash2 / cash;
  80.  
  81. avgCard = moneyCard2 / card;
  82.  
  83. console.log(`Average CS: ${avgCash.toFixed(2)}`);
  84. console.log(`Average CC: ${avgCard.toFixed(2)}`);
  85.  
  86. break;
  87.  
  88.  
  89. }
  90.  
  91. index++;
  92.  
  93. } // While loop
  94.  
  95.  
  96. if (input[index] === 'End') {
  97.  
  98. console.log(`Failed to collect required money for charity.`);
  99.  
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement