didkoslawow

Untitled

Oct 11th, 2022
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. function vacationLoops(input) {
  2. let moneyNeeded = Number(input[0]);
  3. let moneyAvailable = Number(input[1]);
  4. let i = 2;
  5. let action = input[i];
  6. let currentMoney = 0;
  7. let spendCounter = 0;
  8. let daysCounter = 0;
  9.  
  10. while (i < input.length) {
  11. action = String(input[i]);
  12. daysCounter++;
  13. if (action === "spend") {
  14. i++;
  15. currentMoney = Number(input[i]);
  16. moneyAvailable -= currentMoney;
  17. if (moneyAvailable <= 0) {
  18. moneyAvailable = 0;
  19. }
  20. spendCounter++;
  21. if (spendCounter === 5) {
  22. console.log("You can't save the money.");
  23. console.log(spendCounter);
  24. break;
  25. }
  26. } else {
  27. spendCounter = 0;
  28. i++;
  29. currentMoney = Number(input[i]);
  30. moneyAvailable += currentMoney;
  31. if (moneyAvailable >= moneyNeeded) {
  32. console.log(`You saved the money for ${daysCounter} days.`);
  33. break;
  34. }
  35. }
  36. i++;
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment