TZinovieva

Vacation

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