vborislavova

04. Walking - while - loops - ex

Mar 4th, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function walking(input) {
  2.    
  3.     const goal = 10000;
  4.     let steps = 0;
  5.     let isGoingHome = false;
  6.    
  7.     let currentSteps = input.shift();
  8.  
  9.     while ((steps < goal)) {
  10.        
  11.         if (currentSteps === 'Going home') {
  12.             isGoingHome = true;
  13.             currentSteps = input.shift();
  14.             continue;
  15.         } else if (isGoingHome) {
  16.             steps += Number(currentSteps);
  17.             break;
  18.         }
  19.  
  20.         steps += Number(currentSteps);
  21.         currentSteps = input.shift();
  22.     }
  23.  
  24.     if (steps >= goal) {
  25.         console.log(`Goal reached! Good job!`);
  26.     } else {
  27.         let moreSteps = goal - steps;
  28.         console.log(`${moreSteps} more steps to reach goal.`);
  29.     }
  30. }
Add Comment
Please, Sign In to add comment