Advertisement
ErolKZ

Untitled

Jul 4th, 2021
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1.  
  2. function solve(input) {
  3.  
  4. let targetSteps = 10000;
  5.  
  6. let stepsSheMade = 0;
  7.  
  8. let difference = 0;
  9.  
  10. let index = 0;
  11.  
  12.  
  13. while (input[index] !== undefined) {
  14.  
  15.  
  16.  
  17. if (!isNaN(input[index])) {
  18.  
  19. stepsSheMade += Number(input[index]);
  20.  
  21. }
  22.  
  23.  
  24. if (stepsSheMade >= targetSteps && input[index] !== `Going home`) {
  25.  
  26. difference = stepsSheMade - targetSteps;
  27.  
  28. console.log('Goal reached! Good job!');
  29. console.log(`${difference} steps over the goal!`);
  30.  
  31. }
  32.  
  33.  
  34.  
  35. if (input[index] === `Going home`) {
  36.  
  37.  
  38. index = index + 1;
  39.  
  40. stepsSheMade += Number(input[index]);
  41.  
  42. difference = stepsSheMade - targetSteps;
  43.  
  44. difference = Math.abs(difference);
  45.  
  46.  
  47. console.log(`${difference} more steps to reach goal.`);
  48.  
  49. break;
  50.  
  51.  
  52. }
  53.  
  54.  
  55.  
  56. index++;
  57.  
  58. }
  59.  
  60.  
  61.  
  62. }
  63.  
  64. solve([
  65.  
  66. '1500',
  67. '3000',
  68. '250',
  69. '1548',
  70. '2000',
  71. 'Going home',
  72. '2000'
  73.  
  74.  
  75. // '1500',
  76. // '300',
  77. // '2500',
  78. // '3000',
  79. // 'Going home',
  80. // '200'
  81.  
  82.  
  83. // '1000',
  84. // '1500',
  85. // '2000',
  86. // '6500'
  87.  
  88.  
  89. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement