Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function demo(input) {
- let index = 2
- let sumSaveSpend = 0
- let daysSpending = 0
- let days = 0
- let moneyNeeded = Number(input[0])
- let currentMoney = Number(input[1])
- while (index !== input.length) {
- if(input[index] == "spend"){
- sumSaveSpend = Number(input[index + 1])
- daysSpending++
- days++
- if(currentMoney - sumSaveSpend <= 0){
- currentMoney = 0
- }
- else{
- currentMoney-=sumSaveSpend;
- }
- }
- else if(input[index] == "save"){
- sumSaveSpend = Number(input[index + 1])
- daysSpending = 0
- days++
- currentMoney+=sumSaveSpend;
- }
- if(daysSpending == 5){
- console.log(`You can't save the money.`);
- console.log(days);
- break;
- }
- if(currentMoney >= moneyNeeded){
- console.log(`You saved the money for ${days} days.`);
- break;
- }
- index+=2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement