Advertisement
Liliana797979

viarno reshenie steps with while loop

Feb 12th, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function step(input) {
  2.     let steps = 0;
  3.     let goingHome = input[input.length - 2];
  4.     let stepsGoal = 10000;
  5.  
  6.     if (goingHome === "Going home") {
  7.         let i = 0;
  8.  
  9.         while (i < input.length - 2) {
  10.             let currentSteps = Number(input[i]);
  11.             steps += currentSteps;
  12.             i++;
  13.         }
  14.         steps += Number(input[input.length - 1]);
  15.     } else {
  16.         let i = 0;
  17.  
  18.         while (i < input.length) {
  19.             let currentSteps = Number(input[i]);
  20.             steps += currentSteps;
  21.             i++;
  22.  
  23.         }
  24.     }
  25.     if (steps >= stepsGoal) {
  26.         console.log(`Goal reached! Good job!`);
  27.         console.log(`${steps - stepsGoal} steps over the goal!`);
  28.     } else {
  29.         console.log(`${stepsGoal - steps} more steps to reach goal.`);
  30.     }
  31. }
  32.  
  33. step(["125",
  34.     "250",
  35.     "4000",
  36.     "30",
  37.     "2678",
  38.     "4682"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement