elena1234

Time to walk- hours, minutes, seconds ( JavaScript )

Aug 5th, 2021 (edited)
1,174
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function calculateTime(steps, stepsLengthInmeters, speedInKmPerHour ){
  2.     const timeForOneBreak = 500;
  3.     let speedInMetersInSecond = speedInKmPerHour * (1000 / 3600);
  4.     let distanceInMeters = steps * stepsLengthInmeters;  
  5.     let numberOfBreaks = Math.floor(distanceInMeters / timeForOneBreak);
  6.     let timeForBreaksInSeconds = numberOfBreaks * 60;
  7.     let totalTimeInSeconds = Math.round((distanceInMeters / speedInMetersInSecond ) + timeForBreaksInSeconds);
  8.     let totalTimeInMinutes = totalTimeInSeconds / 60;
  9.  
  10.     let hours = Math.floor(totalTimeInMinutes / 60); // hours
  11.     let minutes = Math.floor(totalTimeInMinutes % 60); // minutes
  12.     let seconds = Math.round((totalTimeInMinutes - minutes) * 60); // seconds  
  13.  
  14. console.log((hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes+ ":" + (seconds < 10 ? "0" : "") + seconds);
  15. }
  16.  
  17. calculateTime(4000, 0.60, 5)
RAW Paste Data