Advertisement
Didart

Vacation

Apr 9th, 2022
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function vacation(input) {
  2.  
  3.     let needMoney = Number(input[0]);
  4.     let availableMoney = Number(input[1]);
  5.  
  6.     let index = 2;
  7.     let command = input[index];
  8.  
  9.     let totalDays = 0;
  10.     let spendDays = 0;
  11.  
  12.     while (availableMoney < needMoney) {
  13.         totalDays++;
  14.         index++;
  15.  
  16.         if (command === 'spend') {
  17.             spendDays++;
  18.  
  19.             if (spendDays === 5) {
  20.                 console.log(`You can't save the money.`);
  21.                 console.log(totalDays);
  22.                 break;
  23.             }
  24.  
  25.             let moneyToSpend = Number(input[index]);
  26.             availableMoney -= moneyToSpend;
  27.  
  28.             if (availableMoney < 0) {
  29.                 availableMoney = 0;
  30.             }
  31.            
  32.         } else if (command === 'save') {
  33.             spendDays = 0;
  34.             let moneyToSave = Number(input[index]);
  35.             availableMoney += moneyToSave;
  36.         }
  37.  
  38.         index++;
  39.         command = input[index];
  40.     }
  41.  
  42.     if (availableMoney >= needMoney) {
  43.         console.log(`You saved the money for ${totalDays} days.`);
  44.     }
  45.  
  46. }
  47.  
  48. vacation(["2000", "1000", "spend", "1200", "save", "2000"])
  49.  
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement