Advertisement
Liliana797979

viarno reshenie vacantion

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