Advertisement
ErolKZ

Untitled

Jul 3rd, 2021
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 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 counter = 0;
  13.  
  14. let counter2 = 0;
  15.  
  16.  
  17. while (input[index] !== undefined) {
  18.  
  19. counter2 += 1;
  20.  
  21. if (input[index] === 'spend') {
  22.  
  23. counter += 1;
  24.  
  25. if (moneySheHave < Number(input[index + 1])) {
  26.  
  27. moneySheHave = 0;
  28.  
  29. } else {
  30.  
  31. moneySheHave = moneySheHave - Number(input[index + 1]);
  32.  
  33. }
  34.  
  35. if (counter > 4) {
  36.  
  37. console.log(`You can't save the money.`);
  38. console.log(counter);
  39.  
  40. break;
  41.  
  42. }
  43.  
  44. } else if (input[index] === 'save') {
  45.  
  46.  
  47.  
  48. moneySheHave += Number(input[index + 1]);
  49.  
  50. if (moneySheHave >= moneyNeededForEcur) {
  51.  
  52. console.log(`You saved the money for ${counter2} days.`);
  53.  
  54. break;
  55.  
  56. }
  57.  
  58.  
  59.  
  60. }
  61.  
  62. index = index + 2;
  63.  
  64. } // While loop
  65.  
  66.  
  67. }
  68.  
  69. solve([
  70.  
  71.  
  72. // '2000',
  73. // '1000',
  74.  
  75. // 'spend',
  76. // '1200',
  77. // 'save',
  78. // '2000'
  79.  
  80.  
  81. '250',
  82. '150',
  83. 'spend',
  84. '50',
  85. 'spend',
  86. '50',
  87. 'save',
  88. '100',
  89. 'save',
  90. '100'
  91.  
  92.  
  93. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement