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