safsaf2020

PB April - Vacation Problem

May 17th, 2020
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. function solution(inputData) {
  2.  
  3. let vacationExpenses = Number(inputData[0]);
  4. let amount = Number(inputData[1]);
  5.  
  6. let index = 2;
  7. let days = 0;
  8. let spendMoneyDays = 0;
  9.  
  10. let currentAction = inputData[index];
  11. let currentAmount = Number(inputData[index + 1]);
  12.  
  13. while ((currentAction !== undefined) && (spendMoneyDays < 5)) {
  14.  
  15. if (currentAction === 'spend') {
  16. amount = Math.max((amount - currentAmount), 0);
  17. spendMoneyDays++;
  18. } else if (currentAction === 'save') {
  19. amount += currentAmount;
  20. spendMoneyDays = 0;
  21. }
  22.  
  23. index += 2;
  24. days++;
  25.  
  26. currentAction = inputData[index];
  27. currentAmount = inputData[index + 1];
  28. }
  29.  
  30. if (spendMoneyDays >= 5) {
  31. console.log("You can't save the money.");
  32. console.log(`${days}`);
  33.  
  34. } else if (amount >= vacationExpenses) {
  35.  
  36. console.log(`You saved the money for ${days} days.`);
  37. }
  38. }
Add Comment
Please, Sign In to add comment