Creating a BMI and Caloric Needs Calculator Using JavaScript
In today’s world, staying fit and healthy has become a priority for many individuals. With the rise of fitness apps and online health resources, personalized workout plans are more accessible than ever. This article will guide you through creating a user-friendly fitness tracker web app using HTML, CSS, and JavaScript. By the end, you’ll be able to calculate BMI, BMR, and daily caloric needs while generating personalized workout recommendations based on age, gender, weight, height, and activity level.
Understanding the Basics: What is BMI and BMR?
BMI, or Body Mass Index, is a simple calculation that can help assess whether a person has a healthy body weight for their height. BMR, or Basal Metabolic Rate, is the number of calories your body needs at rest to maintain basic bodily functions. Knowing these values is crucial for anyone looking to manage their weight or improve their fitness.
Setting Up Your Project
Before diving into the code, let’s set up the basic structure of our web app. Create a new directory for your project and inside it, create three files: index.html
, styles.css
, and script.js
.
HTML Structure
In your index.html
file, start by creating a simple form for user input. Here’s a basic structure:
BMI and Caloric Needs Calculator
Fitness Tracker
Male
Female
Sedentary
Light Activity
Moderate Activity
Active
Styling with CSS
Now that we have our HTML structure, let’s add some styles in the styles.css
file to make our app look nice:
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
h1 {
text-align: center;
}
form {
background: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
max-width: 400px;
margin: auto;
}
label {
display: block;
margin: 10px 0 5px;
}
input, select {
width: 100%;
padding: 10px;
margin-bottom: 10px;
}
JavaScript Functionality
Finally, let’s implement the logic to calculate BMI and caloric needs in the script.js
file:
document.getElementById('fitness-form').addEventListener('submit', function(event) {
event.preventDefault();
const age = parseInt(document.getElementById('age').value);
const weight = parseFloat(document.getElementById('weight').value);
const height = parseFloat(document.getElementById('height').value);
const gender = document.getElementById('gender').value;
const activityLevel = document.getElementById('activity').value;
// Calculating BMI
const bmi = (weight / ((height / 100) ** 2)).toFixed(2);
// Calculating BMR
let bmr;
if (gender === 'male') {
bmr = 88.36 + (13.4 * weight) + (4.8 * height) - (5.7 * age);
} else {
bmr = 447.6 + (9.25 * weight) + (3.1 * height) - (4.3 * age);
}
// Adjusting BMR based on activity level
let caloricNeeds;
switch (activityLevel) {
case 'sedentary':
caloricNeeds = (bmr * 1.2).toFixed(2);
break;
case 'light':
caloricNeeds = (bmr * 1.375).toFixed(2);
break;
case 'moderate':
caloricNeeds = (bmr * 1.55).toFixed(2);
break;
case 'active':
caloricNeeds = (bmr * 1.725).toFixed(2);
break;
}
// Displaying results
document.getElementById('results').innerHTML = `
Your Results
BMI: ${bmi}
BMR: ${bmr.toFixed(2)} calories/day
Daily Caloric Needs: ${caloricNeeds} calories/day
`;
});
Conclusion
Congratulations! You’ve just built your own BMI and caloric needs calculator using HTML, CSS, and JavaScript. This simple fitness tracker can help users understand their health better and make informed decisions about their fitness journey. Feel free to expand on this project by adding more features like personalized workout plans or dietary suggestions!