ProdanTenev

Vacation

Mar 2nd, 2022
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 1.00 KB | None | 0 0
  1. function vacation(input) {
  2.     let index = 0;
  3.     let neededMoney = Number(input[index]);
  4.     index++;
  5.     let currentMoney = Number(input[index]);
  6.     index++;
  7.     let days = 0;
  8.     let spendDays = 0;
  9.     while (currentMoney < neededMoney) {
  10.         let action = input[index];
  11.         index++;
  12.         let sum = Number(input[index]);
  13.         index++;
  14.         days++;
  15.         switch (action) {
  16.             case "spend":
  17.                 currentMoney -= sum;
  18.                 spendDays++;
  19.                 if (currentMoney < 0) {
  20.                     currentMoney = 0;
  21.                 }
  22.                 break;
  23.             case "save":
  24.                 currentMoney += sum;
  25.                 spendDays = 0;
  26.                 break;
  27.         }
  28.         if (spendDays === 5) {
  29.             console.log("You can't save the money.");
  30.             console.log(days);
  31.             break;
  32.         }
  33.     }
  34.     if (currentMoney >= neededMoney) {
  35.         console.log(`You saved the money for ${days} days.`);
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment