stefanbats

Softuni - Time to Walk code

Jan 16th, 2021
726
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. function timeToWalk(steps, footprintLength, speed)
  4. {
  5.     let distance = steps * footprintLength;
  6.     let breaks = 0;
  7.    
  8.     for (let index = 500; index < distance + 1; index+= 500) {
  9.         breaks++;
  10.        
  11.     }
  12.  
  13.     let time = (distance / 1000) / speed;
  14.  
  15.    
  16.     let hr = 0;
  17.     let min = 0;
  18.     let sec = 0;
  19.    
  20.    
  21.     //HOURS
  22.     if (time < 1) {
  23.         hr = 0
  24.     }
  25.     else{
  26.         let wholeHrs = time.toString();
  27.         wholeHrs = wholeHrs[0];
  28.         parseInt(wholeHrs);
  29.         hr = wholeHrs;
  30.    
  31.         time -= wholeHrs;
  32.     }
  33.    
  34.    
  35.     //MINUTES
  36.     if (time * 60 < 1) {
  37.         min = 0;
  38.     }
  39.     else{
  40.         min =  time*60;
  41.         min = Math.floor(min);
  42.         time -= min/60
  43.    
  44.         if (min + breaks >= 60) {
  45.             hr++;
  46.             min = min + breaks - 60;
  47.         }
  48.         else
  49.         {
  50.             min += breaks;
  51.         }
  52.     }
  53.    
  54.    
  55.     //SECONDS
  56.     sec = time * 3600;
  57.     sec = Math.round(sec);
  58.    
  59.     console.log(`${('0' + hr).slice(-2)}:${('0' + min).slice(-2)}:${('0' + sec).slice(-2)}`)
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
RAW Paste Data