ErolKZ

Untitled

Jul 5th, 2021
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. function solve(input) {
  2.  
  3.  
  4. let moneyNeeded = Number(input[0]);
  5.  
  6. let moneyWeHave = Number(input[1]);
  7.  
  8. let index = 2;
  9.  
  10. let spendDays = 0;
  11.  
  12. let passedDays = 0;
  13.  
  14.  
  15. while (input[index] !== undefined) {
  16.  
  17. let command = input[index];
  18.  
  19.  
  20.  
  21.  
  22. switch (command) {
  23.  
  24. case 'spend':
  25.  
  26. index++;
  27.  
  28. moneyWeHave -= Number(input[index]);
  29.  
  30. if (moneyWeHave < 0) {
  31.  
  32. moneyWeHave = 0;
  33.  
  34. }
  35.  
  36. spendDays++;
  37.  
  38. break;
  39.  
  40. case 'save':
  41.  
  42. index++;
  43.  
  44. moneyWeHave += Number(input[index]);
  45.  
  46. break;
  47.  
  48.  
  49. }
  50.  
  51.  
  52. passedDays++;
  53.  
  54.  
  55.  
  56. if (spendDays >= 5) {
  57.  
  58. console.log(`You can't save the money.`);
  59. console.log(`${spendDays}`);
  60.  
  61. break;
  62.  
  63. } else if (moneyNeeded <= moneyWeHave) {
  64.  
  65. console.log(`You saved the money for ${passedDays} days.`);
  66.  
  67. break;
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74. index++;
  75.  
  76.  
  77.  
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment