tedo1111

Untitled

Dec 31st, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. function vacation(input) {
  2.  
  3. let needMoney = Number(input[0]);
  4. let moneyInBank = Number(input[1]);
  5.  
  6.  
  7. let move = "";
  8. let money = 0;
  9. dayCounter = 0;
  10. let currentSpendStreak = 0;
  11.  
  12.  
  13. let index = 2;
  14. let currentElement = input[index];
  15.  
  16. while (currentSpendStreak !== 5) {
  17.  
  18. move = currentElement;
  19. index++;
  20. currentElement = input[index];
  21. money = Number(currentElement);
  22. dayCounter++;
  23.  
  24. if (move === `spend`) {
  25. moneyInBank -= money;
  26.  
  27.  
  28.  
  29. }
  30. if (move === "spend") {
  31. currentSpendStreak++;
  32. } else {
  33. currentSpendStreak = 1;
  34. }
  35.  
  36. if (moneyInBank < 0) {
  37. moneyInBank = 0;
  38. }
  39.  
  40. if (move === `save`) {
  41. moneyInBank += money;
  42. }
  43.  
  44. if (needMoney <= moneyInBank) {
  45. console.log(`You saved the money for ${dayCounter} days.`);
  46. break;
  47. }
  48.  
  49. index++;
  50. currentElement = input[index];
  51. }
  52. if (currentSpendStreak === 5) {
  53. console.log(`You can't save the money.\n${dayCounter}`);
  54.  
  55. }
  56.  
  57. }
  58. vacation(["2000", "1000", "spend", "1200", "save", "2000"]);
  59. vacation(["110", "60", "spend", "10", "spend", "10", "spend", "10", "spend", "10", "spend", "10"]);
  60. vacation(["250", "150", "spend", "50", "spend", "50", "save", "100", "save", "100"]);
  61.  
Tags: js
Advertisement
Add Comment
Please, Sign In to add comment