Lesson 7.3 β BMI Calculator Project
π Building a Body Mass Index Calculator Using HTML, CSS & JavaScript
π― Learning Objectives
After completing this lesson, students will be able to:
β Build a BMI Calculator
β Take User Input
β Perform Mathematical Calculations
β Use Conditional Statements
β Use DOM Manipulation
β Display Health Status
β Build Real-World JavaScript Projects
π Introduction
BMI stands for:
βοΈ Body Mass Index
BMI helps determine whether a person is:
β Underweight
β Normal Weight
β Overweight
β Obese
Many websites and fitness applications use BMI calculators.
Examples:
β Health Apps
β Fitness Websites
β Gym Management Systems
β Medical Portals
Today we will build:
βοΈ BMI Calculator Project
π Project Preview
-------------------------
BMI CALCULATOR
Weight (kg)
[ 60 ]
Height (m)
[ 1.65 ]
[ Calculate BMI ]
Your BMI: 22.04
Status: Normal Weight
-------------------------
π― Concepts Used
| Concept | Used |
|---|---|
| Variables | β |
| Operators | β |
| Conditional Statements | β |
| Functions | β |
| Events | β |
| DOM Manipulation | β |
π BMI Formula
BMI is calculated using:
BMI=WeightΒ (kg)Height2Β (m2)BMI=\frac{Weight\ (kg)}{Height^2\ (m^2)}
Example
Weight:
60 kg
Height:
1.65 m
Calculation:
BMI = 60 Γ· (1.65 Γ 1.65)
BMI = 22.04
π BMI Categories
| BMI Value | Category |
|---|---|
| Below 18.5 | Underweight |
| 18.5 β 24.9 | Normal Weight |
| 25 β 29.9 | Overweight |
| 30 and Above | Obese |
ποΈ Project Files
Create:
index.html
style.css
script.js
π― Step 1 β Create HTML Structure
index.html
<!DOCTYPE html>
<html>
<head>
<title>BMI Calculator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>
BMI Calculator
</h1>
<input type="number"
id="weight"
placeholder="Weight (kg)">
<br><br>
<input type="number"
id="height"
placeholder="Height (m)"
step="0.01">
<br><br>
<button onclick="calculateBMI()">
Calculate BMI
</button>
<h2 id="result">
Your BMI Will Appear Here
</h2>
</div>
<script src="script.js"></script>
</body>
</html>
π Understanding HTML
Weight Input:
<input type="number" id="weight">
Height Input:
<input type="number" id="height">
Button:
<button onclick="calculateBMI()">
Result Area:
<h2 id="result">
π― Step 2 β Design the Interface
style.css
body{
font-family:Arial;
background:#f0f0f0;
display:flex;
justify-content:center;
align-items:center;
height:100vh;
}
.container{
background:white;
padding:30px;
border-radius:10px;
box-shadow:0 0 15px lightgray;
text-align:center;
width:350px;
}
input{
padding:10px;
width:250px;
font-size:16px;
}
button{
padding:10px 20px;
background:#0d6efd;
color:white;
border:none;
border-radius:5px;
cursor:pointer;
}
button:hover{
background:#084298;
}
h1{
color:#0d6efd;
}
π― Step 3 β Create JavaScript Function
script.js
function calculateBMI()
{
}
π Step 4 β Get User Input
let weight =
document.getElementById("weight").value;
let height =
document.getElementById("height").value;
Example:
Weight = 60
Height = 1.65
π― Step 5 β Validate Input
if(weight == "" || height == "")
{
document.getElementById("result").innerHTML =
"Please Enter Weight and Height";
return;
}
Output
Please Enter Weight and Height
π― Step 6 β Calculate BMI
let bmi =
weight / (height * height);
Example:
60 Γ· (1.65 Γ 1.65)
= 22.04
π Round the Result
bmi = bmi.toFixed(2);
Output:
22.04
π― Step 7 β Determine Health Status
let status = "";
Underweight
if(bmi < 18.5)
{
status = "Underweight";
}
Normal Weight
else if(bmi < 25)
{
status = "Normal Weight";
}
Overweight
else if(bmi < 30)
{
status = "Overweight";
}
Obese
else
{
status = "Obese";
}
π― Step 8 β Display Result
document.getElementById("result").innerHTML =
"Your BMI: " + bmi +
"<br>Status: " + status;
π Complete JavaScript Code
script.js
function calculateBMI()
{
let weight =
document.getElementById("weight").value;
let height =
document.getElementById("height").value;
if(weight == "" || height == "")
{
document.getElementById("result").innerHTML =
"Please Enter Weight and Height";
return;
}
let bmi =
weight / (height * height);
bmi = bmi.toFixed(2);
let status = "";
if(bmi < 18.5)
{
status = "Underweight";
}
else if(bmi < 25)
{
status = "Normal Weight";
}
else if(bmi < 30)
{
status = "Overweight";
}
else
{
status = "Obese";
}
document.getElementById("result").innerHTML =
"Your BMI: " + bmi +
"<br>Status: " + status;
}
π» Complete Project Code
index.html
<!DOCTYPE html>
<html>
<head>
<title>BMI Calculator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>BMI Calculator</h1>
<input type="number"
id="weight"
placeholder="Weight (kg)">
<br><br>
<input type="number"
id="height"
placeholder="Height (m)"
step="0.01">
<br><br>
<button onclick="calculateBMI()">
Calculate BMI
</button>
<h2 id="result">
Your BMI Will Appear Here
</h2>
</div>
<script src="script.js"></script>
</body>
</html>
script.js
function calculateBMI()
{
let weight =
document.getElementById("weight").value;
let height =
document.getElementById("height").value;
if(weight == "" || height == "")
{
document.getElementById("result").innerHTML =
"Please Enter Weight and Height";
return;
}
let bmi =
weight / (height * height);
bmi = bmi.toFixed(2);
let status = "";
if(bmi < 18.5)
{
status = "Underweight";
}
else if(bmi < 25)
{
status = "Normal Weight";
}
else if(bmi < 30)
{
status = "Overweight";
}
else
{
status = "Obese";
}
document.getElementById("result").innerHTML =
"Your BMI: " + bmi +
"<br>Status: " + status;
}
π― Expected Output
Input:
Weight = 60
Height = 1.65
Output:
Your BMI: 22.04
Status: Normal Weight
π Project Improvements
Add:
β Reset Button
β BMI Color Indicators
β Health Tips
β Ideal Weight Range
β Dark Mode
π« Real World Applications
BMI Calculators are used in:
β Hospitals
β Fitness Centers
β Health Apps
β Gym Management Systems
β Medical Portals
β Wellness Websites
β οΈ Common Beginner Mistakes
Mistake 1
Using Height in Centimeters
Wrong:
165
Correct:
1.65
Mistake 2
Forgetting Validation
May produce invalid results.
Mistake 3
Not Using toFixed()
Results may show many decimal places.
π Best Practices
β Validate Input
β Round BMI Values
β Display Clear Messages
β Use Meaningful Variable Names
β Keep UI Simple
π Project Challenge
Modify the project and add:
β User Name Input
β Age Input
β Gender Selection
β Personalized Health Advice
β BMI Progress Bar
π Practical Activity
Create:
Student Fitness Checker
Inputs:
- Student Name
- Weight
- Height
Output:
- BMI
- Health Status
- Fitness Suggestion
π Assignment
Assignment 7.3
- What is BMI?
- Write the BMI Formula.
- Why is input validation important?
- What does
toFixed(2)do? - Create a BMI Calculator project.
- Add BMI categories.
- Add a Reset Button.
π§ Quiz
Q1. BMI stands for?
A. Body Mass Index
B. Body Measurement Indicator
C. Body Meter Index
D. Body Mass Information
β Answer: A
Q2. Which formula is used for BMI?
A. Weight Γ Height
B. Weight Γ· HeightΒ²
C. Height Γ· Weight
D. Weight + Height
β Answer: B
Q3. Which JavaScript method rounds to 2 decimal places?
A. round()
B. fix()
C. toFixed(2)
D. decimal()
β Answer: C
Q4. BMI Calculator uses?
A. DOM Manipulation
B. Functions
C. Conditional Statements
D. All of These
β Answer: D
Q5. A BMI of 22 falls into which category?
A. Underweight
B. Normal Weight
C. Overweight
D. Obese
β Answer: B
π Lesson Summary
β Built a complete BMI Calculator.
β Used mathematical calculations.
β Used conditional statements to determine health status.
β Applied DOM Manipulation for dynamic output.
β Created a real-world health application.