Advertisement
Guest User

Untitled

a guest
May 10th, 2020
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.   let moneyNeeded = +input.shift();
  3.   let moneyNow = +input.shift();
  4.   let counterDays = 0;
  5.   let counterSpend = 0;
  6.  
  7.   while (moneyNow < moneyNeeded && counterSpend < 5) {
  8.     let action = input.shift();
  9.     let amountToAction = +input.shift();
  10.  
  11.     if (action == "save") {
  12.       moneyNow += amountToAction;
  13.       counterSpend = 0;
  14.     } else if (action == "spend") {
  15.       counterSpend++;
  16.       moneyNow -= amountToAction;
  17.     }
  18.     if (moneyNow < 0) {
  19.       moneyNow = 0;
  20.     }
  21.  
  22.     counterDays++;
  23.   }
  24.   if (moneyNeeded <= moneyNow) {
  25.     console.log(`You saved the money for ${counterDays} days.`);
  26.   }
  27.   if (counterSpend == 5) {
  28.     console.log("You can't save the money.");
  29.     console.log(counterDays);
  30.   }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement