Advertisement
Spocoman

03. Vacation

Dec 28th, 2021 (edited)
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function vacation(input) {
  2.     let moneyNeeded = Number(input[0]);
  3.     let budget = Number(input[1]);
  4.     let spendDay = 0, saveDays = 0, index = 2;
  5.  
  6.     while (moneyNeeded > budget && spendDay !== 5) {
  7.         let command = input[index++];
  8.         let money = Number(input[index++]);
  9.         if (command === "spend") {
  10.             budget -= money;
  11.             if (budget < 0) {
  12.                 budget = 0;
  13.             }
  14.             spendDay++;
  15.         } else {
  16.             budget += money;
  17.             spendDay = 0;
  18.         }
  19.         saveDays++;
  20.     }
  21.  
  22.     if (budget < moneyNeeded) {
  23.         console.log(`You can't save the money.\n${saveDays}`)
  24.    } else {
  25.        console.log(`You saved the money for ${saveDays} days.`);
  26.    }
  27.    return;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement