Advertisement
Guest User

Vacation

a guest
Nov 7th, 2019
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function holiday(input) {
  2.     let neededMoney = Number(input.shift());
  3.     let ownedMoney = Number(input.shift());
  4.     let spendCounter = 0;
  5.     let dayCounter = 0;
  6.  
  7.     while (ownedMoney <= neededMoney && spendCounter <= 5) {
  8.         let operation = input.shift();
  9.         let money = Number(input.shift());
  10.  
  11.         if (operation === 'save') {
  12.             dayCounter++;
  13.             ownedMoney += money;
  14.  
  15.             if (ownedMoney >= neededMoney) {
  16.                 console.log(`You saved the money for ${dayCounter} days.`);
  17.                 break;
  18.             }
  19.  
  20.         } else if (operation === 'spend') {
  21.             spendCounter++;
  22.             dayCounter++;
  23.             ownedMoney -= money;
  24.  
  25.             if (ownedMoney <= 0) {
  26.                 ownedMoney = 0;
  27.             }
  28.  
  29.             if (spendCounter === 5) {
  30.                 console.log(`You can't save the money.`);
  31.                console.log(`${dayCounter}`);
  32.                break;
  33.            }
  34.        }
  35.  
  36.    }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement