"Building a stride calculator with plain JavaScript"

2026-11-03来源 dev.to BeginnersEnglish

****Fitness trackers often use one default stride length for everyone, so the distance they show can be wrong even when the step count is right. I wanted a small tool that estimates stride length from height, so I built one.
It is plain HTML, CSS and JavaScript. No framework, no build step, no backend. It runs in the browser and is hosted for free on GitHub Pages.
Try it: Stride Length Calculator
The idea
For walking, a common rule of thumb says your step length is about 0.413 × height for women and 0.415 × height for men. The two multipliers differ by under 0.5%, so an average of 0.414 works for most people. Stride length is two steps.
For running, the step is longer. Roughly 0.55 to 0.65 × height at an easy pace and 0.70 to 0.80 × height at race pace. These are estimates, and real values change with speed and the runner.
The core code
The whole calculation fits in a few lines:
const WALK = { women: 0.413, men: 0.415, average: 0.414 };
const INCHES_PER_MILE = 63360;

function walkingStats(heightInches, group = "average") {
const step = heightInches * WALK[group]; // inches per step
const stride = step * 2; // two steps
const stepsPerMile = INCHES_PER_MILE / step;
return { step, stride, stepsPerMile };
}

console.log(walkingStats(66));
// step ≈ 27.3 in, stride ≈ 54.6 in, about 2,320 steps per mile
A 5'6" (66 in) walker has a step of about 27.3 inches and takes roughly 2,320 steps per mile. A 5'4" walker takes about 2,390 and a 5'9" walker about 2,220.
What I learned building it

  1. Terms matter more than maths. "Step length" and "stride length" are different things, but many apps use "stride" to mean one step. I added a plain explanation and a diagram, because mixing them up doubles or halves your result.
  2. Show the formula next to the answer. People trust a calculator more when it shows how the result was produced, so every result displays the steps and the multiplier.
  3. Units are easy to get wrong. Height can arrive in cm, in feet and inches, or in inches only, so each input needs its own handling and a test. Conversions are a likelier source of bugs than the formula itself.
  4. A static site is enough. For a tool like this, a static page loads fast, costs nothing to host, and never uploads the user's data, because the maths runs in the browser.
  5. Estimates need honest wording. The formula is a rule of thumb. For a better number, walk a known distance, count your steps and divide. I put that method on the page too. Measure it yourself Mark 10 metres of flat ground. Walk it at a normal pace and count your steps. Divide 10 metres by your step count, and repeat three times. For example, 14 steps over 10 metres is 0.71 m per step, which matches the formula for someone about 172 cm tall. Feedback welcome If you spot a bug, a wrong number, or a missing feature (a running mode, a metric-only view), tell me in the comments. I'd also like to hear which unit-handling mistakes you've hit in your own tools. The tool and the guides behind it are at toolpond.org.

原文:https://www.a8q9.cn/en/article/building-a-stride-calculator-with-plain-javascript-d99bba.html

相关文章