Urgent.News

What's breaking now, across thousands of outlets.

Tech

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…

I did not set out to build a health app. I simply wanted a concise weekend project, devoid of to-do lists, that could deliver real inputs, real math, and an answer I could validate against my own life. Thus, I crafted a compact calorie calculator using plain JavaScript, sans any framework or build step—just a form, a handful of inputs, and one function.

The formula utilized The starting point for most calculators is the Mifflin-St Jeor equation, which estimates one's basal metabolic rate, approximately how many calories one's body burns while at rest.

The JavaScript code begins with the bmr function, which takes weight in kilograms, height in centimeters, age, and sex as inputs. It calculates the base metabolic rate by multiplying the weight by 10, height by 6.25, subtracting five times the age, and adding or subtracting 5 based on sex. The maintenance function then multiplies the bmrValue by an activity factor to provide an estimate for daily calorie maintenance, which helps maintain weight stability.

Testing the calculator on myself, the initial bug was mine, not the code. I entered my height in the incorrect unit, yielding a value suitable for a small horse, prompting a moment of self-questioning. Upon rectifying this, the result indeed surprised me. My maintenance estimate was higher than anticipated, contrary to my initial assumption of needing to consume fewer calories.

This revelation stemmed from an online calorie guide, highlighting the significant impact of the activity multiplier on the outcome—a change of several hundred calories, roughly equivalent to an entire meal. This discovery underscored the critical role the multiplier plays in determining daily caloric needs, far more than initially realized.

The formula, though comprising just ten lines of code, posed the greater challenge: honesty in inputting one's activity level. I contemplated how actively I was living my life on an average week, not my peak performance week. The takeaway was clear: presenting an estimate as a single, definitive number can be misleading. To improve, I plan to incorporate thorough unit tests for edge cases, inform users of potential errors when height is input in the wrong unit, and instead present a range rather than a single, definitive figure.

Despite its simplicity, this project achieved its goal. I shipped a small tool, gained insight into the workings of a widely quoted equation, and adjusted my eating habits—this time, increasing rather than decreasing my caloric intake, an unexpected but welcome outcome. Have you ever developed a small tool that taught you more about yourself than you anticipated?

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

A Flag Is A Decision Made Before The Service

The service receives a finished yes or no the same way it receives who arrived - not the address of a flag store. 👋 Hi, I'm Anton - a software engineer working mostly in PHP/Symfony and Go, currently…

More from Thursday 8 October →