Advertisement
zarkoto223

vacation

Feb 26th, 2024
1,133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function asd(input) {
  2.     let neededMoney = Number(input[0])
  3.     let startMoney = Number(input[1])
  4.  
  5.     let daysSpend = 0
  6.     let savedMoney = startMoney
  7.     let days = 0
  8.  
  9.     let index = 2
  10.     let curentDayAction = input[index]
  11.     let curentDayMoney = Number(input[index + 1])
  12.     while (savedMoney < neededMoney) {
  13.         days++
  14.         if (curentDayAction === 'spend') {
  15.             savedMoney -= curentDayMoney
  16.             daysSpend++
  17.             if (savedMoney <= 0) {
  18.                 savedMoney = 0
  19.             }
  20.         }
  21.         if (curentDayAction === 'save') {
  22.             savedMoney += curentDayMoney
  23.         }
  24.         if (daysSpend === 5) {
  25.             console.log(`You can't save the money.`)
  26.            console.log(`${days}`)
  27.            break
  28.  
  29.        }
  30.  
  31.        index += 2
  32.        curentDayAction = input[index]
  33.        curentDayMoney = Number(input[index + 1])
  34.  
  35.    }
  36.    if (savedMoney >= neededMoney) {
  37.        console.log(`You saved the money for ${days} days.`)
  38.    }
  39.  
  40.  
  41. }
  42. asd(["2000",
  43.    "1000",
  44.    "spend",
  45.    "1200",
  46.    "save",
  47.    "2000"])
  48.  
  49. // asd(["110",
  50. // "60",
  51. // "spend",
  52. // "10",
  53. // "spend",
  54. // "10",
  55. // "spend",
  56. // "10",
  57. // "spend",
  58. // "10",
  59. // "spend",
  60. // "10"])
  61.  
  62. // asd(["250",
  63. // "150",
  64. // "spend",
  65. // "50",
  66. // "spend",
  67. // "50",
  68. // "save",
  69. // "100",
  70. // "save",
  71. // "100"])
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement