DEV Community

Mike
Mike

Posted on

I Built a Tiny Calorie Calculator (and What the Numbers Taught Me)

I did not set out to build a health app. I just wanted a small weekend project that was not another to-do list — something with real inputs, real maths, and an answer I could actually check against my own life.

So I built a tiny calorie calculator in plain JavaScript. No framework, no build step, just a form, a few inputs, and one function.

The formula I used

Most calculators start with the Mifflin-St Jeor equation, which estimates your basal metabolic rate — roughly how many calories your body burns at rest.

function bmr(weightKg, heightCm, age, isMale) {
  const base = 10 * weightKg + 6.25 * heightCm - 5 * age;
  return isMale ? base + 5 : base - 161;
}

function maintenance(bmrValue, activity) {
  return Math.round(bmrValue * activity);
}
Enter fullscreen mode Exit fullscreen mode

From there you multiply by an activity factor — around 1.2 if you sit at a desk most of the day, higher if you train a lot — and you get an estimate for your maintenance calories: the amount that should keep your weight roughly stable.

Testing it on the only user I had: me

The first bug was mine, not the code's. I entered my height in the wrong unit, got a number that would suit a small horse, and briefly questioned everything. Once I fixed that, the result genuinely surprised me.

My maintenance estimate came out higher than I expected. I had been assuming I needed to eat much less, mostly based on vibes and random numbers I had seen online. While testing my numbers against a calories guide I realised the activity multiplier matters far more than I thought — changing it moved my result by several hundred calories, which is basically a whole meal.

That was the interesting lesson. The formula itself is about ten lines. The hard part is being honest about the inputs: how active am I really, on a normal week, not my best week?

What I would do differently

If I rebuild this, I will add proper unit tests for edge cases, warn when someone enters a height that is clearly in metres instead of centimetres, and show a range instead of one confident-looking number. An estimate presented as a single exact figure feels more certain than it really is.

Still, as a tiny project it did its job. I shipped something small, I learned how an equation people quote everywhere actually works, and I came away eating a bit more sensibly — not less, which is not what I expected when I started typing.

Have you built a small tool that ended up teaching you more about yourself than about code?

Top comments (0)