Course Content
Project file
0/1
Complete Web Designing Course for Beginners

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

  1. What is BMI?
  2. Write the BMI Formula.
  3. Why is input validation important?
  4. What does toFixed(2) do?
  5. Create a BMI Calculator project.
  6. Add BMI categories.
  7. 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.

Scroll to Top