Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function asd(input) {
- let neededMoney = Number(input[0])
- let startMoney = Number(input[1])
- let daysSpend = 0
- let savedMoney = startMoney
- let days = 0
- let index = 2
- let curentDayAction = input[index]
- let curentDayMoney = Number(input[index + 1])
- while (savedMoney < neededMoney) {
- days++
- if (curentDayAction === 'spend') {
- savedMoney -= curentDayMoney
- daysSpend++
- if (savedMoney <= 0) {
- savedMoney = 0
- }
- }
- if (curentDayAction === 'save') {
- savedMoney += curentDayMoney
- }
- if (daysSpend === 5) {
- console.log(`You can't save the money.`)
- console.log(`${days}`)
- break
- }
- index += 2
- curentDayAction = input[index]
- curentDayMoney = Number(input[index + 1])
- }
- if (savedMoney >= neededMoney) {
- console.log(`You saved the money for ${days} days.`)
- }
- }
- asd(["2000",
- "1000",
- "spend",
- "1200",
- "save",
- "2000"])
- // asd(["110",
- // "60",
- // "spend",
- // "10",
- // "spend",
- // "10",
- // "spend",
- // "10",
- // "spend",
- // "10",
- // "spend",
- // "10"])
- // asd(["250",
- // "150",
- // "spend",
- // "50",
- // "spend",
- // "50",
- // "save",
- // "100",
- // "save",
- // "100"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement