Advertisement
Didart

Walking

Apr 9th, 2022
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function walking(input) {
  2.    
  3.    let target = 10000;
  4.  
  5.    let sum = 0;
  6.    let index = 0;
  7.  
  8.    let command = input[index];
  9.  
  10.    while (command !== 'Going home') {
  11.       let steps = Number(command);
  12.       sum += steps;
  13.  
  14.       if (sum >= target) {
  15.          console.log(`Goal reached! Good job!`);
  16.          console.log(`${sum - target} steps over the goal!`);
  17.          break;
  18.       }
  19.  
  20.       index++;
  21.       command = input[index];
  22.    }
  23.  
  24.    if (command === "Going home") {
  25.       let stepsToHome = Number(input[index + 1]);
  26.       sum += stepsToHome;
  27.  
  28.       if (sum >= target) {
  29.          console.log(`Goal reached! Good job!`);
  30.          console.log(`${sum - target} steps over the goal!`);
  31.       } else {
  32.          console.log(`${target - sum} more steps to reach goal. `);
  33.       }
  34.  
  35.    }
  36.    
  37. }
  38.  
  39. walking(["1000", "1500", "2000", "6500"])
  40.  
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement