Advertisement
ErolKZ

Untitled

Jul 4th, 2021
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1.  
  2. function solve(input) {
  3.  
  4.  
  5. let moneyNeededForEcur = Number(input[0]);
  6.  
  7. let moneySheHave = Number(input[1]);
  8.  
  9. let index = 2;
  10.  
  11.  
  12. let spendCounter = 0;
  13.  
  14. let daysCounter = 0;
  15.  
  16.  
  17.  
  18.  
  19. while (input[index] !== undefined) {
  20.  
  21.  
  22. let command = input[index];
  23.  
  24. switch (command) {
  25.  
  26. case 'spend':
  27.  
  28. index++;
  29.  
  30. moneySheHave -= Number(input[index]);
  31.  
  32. if (moneySheHave < 0) {
  33.  
  34. moneySheHave = 0;
  35.  
  36. }
  37.  
  38. spendCounter++;
  39.  
  40. break;
  41.  
  42. case 'save':
  43.  
  44. index++;
  45.  
  46. moneySheHave += Number(input[index]);
  47.  
  48. break;
  49.  
  50.  
  51. } // Switch
  52.  
  53.  
  54. daysCounter++;
  55.  
  56. index++;
  57.  
  58.  
  59.  
  60.  
  61. if (spendCounter >= 5) {
  62.  
  63. console.log(`You can't save the money.`);
  64. console.log(spendCounter);
  65.  
  66. break;
  67.  
  68. } else if (moneyNeededForEcur <= moneySheHave) {
  69.  
  70. console.log(`You saved the money for ${daysCounter} days.`);
  71.  
  72. break;
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. } // While loop
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. }
  89.  
  90. solve([
  91.  
  92.  
  93. '2000',
  94. '1000',
  95.  
  96. 'spend',
  97. '1200',
  98. 'save',
  99. '2000'
  100.  
  101.  
  102. // '250',
  103. // '150',
  104. // 'spend',
  105. // '50',
  106. // 'spend',
  107. // '50',
  108. // 'save',
  109. // '100',
  110. // 'save',
  111. // '100'
  112.  
  113.  
  114. ]);
  115.  
  116.  
  117.  
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement