MartenBG

Untitled

Feb 9th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. function vacation(input) {
  2. let vacantionNeeded = +(input.shift());
  3. let haveMoney = +(input.shift());
  4. let saveCount = 0;
  5. let spendCount = 0;
  6.  
  7. while (haveMoney < vacantionNeeded && spendCount < 5) {
  8. spendOrSaveInput = input.shift();
  9. spendOrSaveMoney = +(input.shift());
  10. if (spendOrSaveInput == "save") {
  11. haveMoney += spendOrSaveMoney;
  12. spendCount = 0;
  13. }
  14. if (spendOrSaveInput == "spend") {
  15. haveMoney -= spendOrSaveMoney;
  16. if (haveMoney < 0) {
  17. haveMoney = 0;
  18. }
  19. spendCount++;
  20. }
  21. saveCount++;
  22. }
  23. if (spendCount == 5) {
  24. console.log(`You can't save the money.\n${spendCount}`);
  25. }
  26. if (haveMoney >= vacantionNeeded) {
  27. console.log(`You saved the money for ${saveCount} days.`);
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment