ProdanTenev

Walking

Mar 2nd, 2022
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 0.82 KB | None | 0 0
  1. function walking(input) {
  2.     index = 0;
  3.     let targetSteps = 10000;
  4.     let summarySteps = 0;
  5.     while (summarySteps < targetSteps) {
  6.         let command = input[index];
  7.         let currentSteps;
  8.         if (command === "Going home") {
  9.             index++;
  10.             currentSteps = Number(input[index]);
  11.             summarySteps += currentSteps;
  12.             break;
  13.         }
  14.         index++;
  15.         currentSteps = Number([command]);
  16.         summarySteps += currentSteps;
  17.     }
  18.     if (summarySteps >= targetSteps) {
  19.         let diff = Math.abs(targetSteps - summarySteps);
  20.         console.log("Goal reached! Good job!");
  21.         console.log(`${diff} steps over the goal!`);
  22.     } else {
  23.         let diff = Math.abs(targetSteps - summarySteps);
  24.         console.log(`${diff} more steps to reach goal.`);
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment