Harris-Benedict formula in JavaScript - Maintenance calories calculator
In this tutorial, we will create a Maintenance calorie calculator that we base on the Harris-Benedict formula. The Harris-Benedict formula will give us our BMR (calories burnt while resting) and then to get our maintenence calories, we will apply a multiplier which represent our activity level.
What does maintenance calories mean?
Simply put, the calorie intake to keep the same weight basically. The value we get can be a good indicator to follow if you want to lose or gain weight. A common value to follow is 500 calorie deficit or surplus to keep/gain 0.5kg (1 lbs) per week.
How to get the multiplier?
In this guide we will follow a general rule that are applied in the fitness world, and it goes like this:
- Little to no exercise: 1.2 * BMR
- Light exercise 1-3 days/week: 1.375 * BMR
- Moderate exercise 3-5 days/week: 1.55 * BMR
- Hard exercise 6-7 days/week: 1.725 * BMR
- Elite training (2x/day) or very physical job: 1.9 * BMR
Implementation
So, let's get to it. We will create our solution to both handle kg and lbs as well as cm and inches. And the formula is expecting values from the metric system (cm and kg), so we need to be able to convert those values if wanted.
Start with conversion of lbs to kg
const lbsToKg = (lbs) => lbs / 2.20462;
And now our inches to cm
const inchesToCm = (inches) => inches * 2.54;
Let's implement the formula
The formula is like following:
For men:
BMR = 66.5 + (13.75 × weight in kg) + (5.003 × height in cm) - (6.75 × age)
For woman:
BMR = 655.1 + (9.563 × weight in kg) + (1.850 × height in cm) - (4.676 × age)
In code:
const maleBMR = (age, kg, cm) => Math.ceil(66.5 + 13.75 * kg + 5.003 * cm - 6.75 * age); const femaleBMR = (age, kg, cm) => Math.ceil(655.1 + 9.563 * kg + 1.85 * cm - 4.676 * age);
Our function to export to the consumers:
/** * calculateBMR - function for calculating BMR * * @param {string} gender - MAN or WOMAN * @param {number} age - Age of person * @param {number} height - Height in cm or inches * @param {number} weight - Weight in kg or lbs * @param {boolean = true} useMetric - Flag for using the metric system, default as true * @returns {number} BMR for the person */ export const calculateBMR = (gender, age, height, weight, useMetric = true) => { const kg = useMetric ? weight : lbsToKg(weight); const cm = useMetric ? height : inchesToCm(height); if (gender === "MAN") { return maleBMR(age, kg, cm); } else if (gender === "WOMAN") { return femaleBMR(age, kg, cm); } throw new Error("Wrong gender provided. Must be WOMAN or MAN"); };
Let's create a simple maintenance function as well
/** * calculateMaintenance - funtion for calculating the maintenance calories * * @param {number} bmr - bmr for the person * @param {number} activityLevel - activity multiplier value * @returns {number} - Maintenance calories */ export const calculateMaintenance = (bmr, activityLevel) => Math.ceil(bmr * activityLevel);
Testing
All right, let's try the formula. We will try with the same values for both woman and men and using both metric and imperial systems.
console.log(calculateBMR("MAN", 25, 180, 70)); // 1761 console.log(calculateBMR("MAN", 25, 70.9, 154.32, false)); // 1762 console.log(calculateBMR("WOMAN", 25, 180, 70)); // 1541 console.log(calculateBMR("WOMAN", 25, 70.9, 154.32, false)); // 1541
Cool. So we got the same values for both systems (almost, since we were doing Math.ceil it diffed a littled bit between the two men calculations).
Now let's try the maintenance calculation for the same people
All right, so we will use the moderate activity level and light exercise level multipliers.
console.log(calculateMaintenance(calculateBMR("MAN", 25, 180, 70), 1.55)); // 2730 calories console.log( calculateMaintenance(calculateBMR("MAN", 25, 70.9, 154.32, false), 1.375) ); // 2423 calories console.log(calculateMaintenance(calculateBMR("WOMAN", 25, 180, 70), 1.55)); // 2389 calories console.log( calculateMaintenance(calculateBMR("WOMAN", 25, 70.9, 154.32, false), 1.375) ); // 2119 calories
There we go folks - now we can start to feel bad when going out for that friday pizza with a cold beer :)
Outro
In this tutorial we implemented the Harris-Benedict formula for calculating BMR, and also the maintenance calories per day for men and woman. We also did some conversion function between metric and imperial systems.
I hope you found this helpful. See you in the next one!