Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. function tripPrice(input) {
  2. let tripPrice = Number(input[0]);
  3. let moneyNow = Number(input[1]);
  4.  
  5. let actionType = input[2];
  6. let sum = Number(input[3]);
  7. let index = 4;
  8. let spendDaysCounter = 0;
  9. let totalDaysCounter = 0;
  10.  
  11. while(moneyNow < tripPrice) {
  12. if(actionType === 'spend') {
  13. spendDaysCounter++
  14. moneyNow -= sum
  15. }
  16. if(moneyNow < 0) {
  17. moneyNow = 0;
  18. }
  19. if(actionType === 'save') {
  20. spendDaysCounter = 0;
  21. moneyNow += sum;
  22. }
  23. totalDaysCounter++
  24.  
  25. if(moneyNow < 0) {
  26. moneyNow = 0;
  27. }
  28. if(spendDaysCounter === 5) {
  29. console.log(`You can't save the money.`);
  30. console.log(`${spendDaysCounter}`);
  31. break;
  32. }
  33.  
  34.  
  35. actionType = input[index];
  36. index++
  37. sum = Number(input[index]);
  38. index++
  39.  
  40. }
  41.  
  42.  
  43. if(moneyNow >= tripPrice) {
  44. console.log(`You saved the money for ${totalDaysCounter} days.`);
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement