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