Advertisement
AlexandrP

03. Vacation (While Loop - Exercise)

Dec 6th, 2022 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function holliday (input) {
  2.  
  3.     let exucrsionPrice = Number (input [0])
  4.     let startMoney = Number (input [1])
  5.  
  6.     let days = 0
  7.     let daysSpent = 0
  8.  
  9.     let action = input [2]
  10.     let moneyAction = input [3]
  11.  
  12.     index = 4
  13.  
  14.     while (exucrsionPrice > startMoney){
  15.         days++
  16.  
  17.         if (action === 'spend'){
  18.             daysSpent++
  19.                 if (moneyAction > startMoney){
  20.                     startMoney=0
  21.                 }else{
  22.                     startMoney = startMoney - moneyAction
  23.                    
  24.                 }  
  25.  
  26.             if (daysSpent >= 5){
  27.                 console.log("You can't save the money.");
  28.                 console.log(days);
  29.                 return
  30.             }
  31.  
  32.         }else{
  33.             startMoney = startMoney + moneyAction
  34.             daysSpent = 0
  35.         }
  36.  
  37.         action = input [index]
  38.         index++
  39.         moneyAction = Number (input [index])
  40.         index++
  41.  
  42.  
  43.     }
  44.  
  45.  
  46. console.log(`You saved the money for ${days} days.`);
  47.  
  48.  
  49. }
  50.  
  51. holliday (["2000",
  52. "1000",
  53. "spend",
  54. "1200",
  55. "save",
  56. "2000"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement