visit
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>BMI CALCULATOR</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div>
<h1 style="text-align: center">BMI CALCULATOR</h1>
<h3>What is BMI?</h3>
<p class="para">
BMI stands for Body Mass Index, which is a measure of body fat based on a person's weight and height. It is commonly used to assess whether a person is underweight, normal weight, overweight, or obese. BMI is calculated by dividing a person's weight (in kilograms) by their height(in meters squared).
</p>
<p class="para">
BMI is a useful tool to assess whether a person is at a healthy weightor not, but it is not always a perfect indicator of health. For example,someone with a high amount of muscle mass may have a high BMI even though they are not overweight or obese. Additionally, BMI does not take into account other factors such as age, sex, or body composition, so it should be used in conjunction with other measures to assess overall
health.
</p>
<h3>Ranges</h3>
<p class="para">
BMI ranges are typically categorized into different levels of bodyweight and are used to indicate whether a person is underweight, normalweight, overweight, or obese. The World Health Organization (WHO) has established the following BMI ranges for adults:
</p>
<ul class="para">
<li>Underweight: BMI below 18.5</li>
<li>Normal weight: BMI between 18.5 and 24.9</li>
<li>Overweight: BMI between 25 and 29.9</li>
<li>Obese: BMI 30 or higher</li>
</ul>
<section class="work">
<h2 class="top">Calculate your BMI</h2>
<p class="weight"><b>Enter Your Weight in kg</b></p>
<input type="number" id="weightInput" />
<p class="height"><b>Enter your height in meters</b></p>
<input type="number" id="heightInput" />
<br />
<button
type="submit
"
id="btn"
>
Calculate
</button>
</section>
<h1 id="result">Your BMI is</h1>
<footer>
<p class="foot" style="text-align: center">
Made with ❤ by SHASHANK SHARMA ©2023
</p>
</footer>
</div>
<script src="script.js"></script>
</body>
</html>
body {
margin: 0;
padding-left: 350px;
padding-right: 350px;
overflow: auto;
box-sizing: border-box;
background-color: #ecf2ff;
}
div {
border: 5px solid black;
padding: 40px;
}
.top {
font-size: 40px;
color: white;
}
.para {
font-size: 18px;
}
.work {
text-align: center;
background-color: #3e54ac;
}
#result {
text-align: center;
}
.weight {
font-size: 24px;
}
.height {
font-size: 24px;
}
.foot {
color: black;
border: 2px solid black;
padding: 20px;
font-size: 20px;
}
#btn {
margin-top: 15px;
padding: 9px 40px;
background-color: black;
color: white;
}
/* CSS MEDIA QUERIES */
@media only screen and (max-width: 600px) {
/* Adjust font sizes */
h1 {
font-size: 2rem;
}
h2 {
font-size: 1.5rem;
}
h3 {
font-size: 1.2rem;
}
p {
font-size: 1rem;
}
/* Center align text */
h1,
h2,
h3,
p {
text-align: center;
}
/* Adjust padding and margins */
body {
padding: 0.5rem;
}
.para {
margin: 0.5rem;
}
/* Adjust input field sizes */
#weightInput,
#heightInput {
width: 100%;
margin-bottom: 0.5rem;
}
}
@media only screen and (min-width: 600px) and (max-width: 1200px) {
/* Adjust font sizes */
h1 {
font-size: 3rem;
}
h2 {
font-size: 2rem;
}
h3 {
font-size: 1.5rem;
}
p {
font-size: 1.2rem;
}
/* Adjust padding and margins */
body {
padding: 1rem;
}
.para {
margin: 1rem;
}
/* Adjust input field sizes */
#weightInput,
#heightInput {
width: 50%;
margin-bottom: 1rem;
}
}
var :- Creating a variable
valueOne :- The name we have given to our variable
document:- The document from which we are selecting the element
getElementById(“ ”) :- Id selector to select the element with a unique ID
(“weightInput”) :- The name of the Id.
var valueOne = document.getElementById("weightInput");
var valueTwo = document.getElementById("heightInput");
I can do the calculation here with two methods:-
Method-1:- by simply retrieving the values, storing them in variables, and doing the calculation.
Method-2:- by using the function()
var weight = document.getElementById("weightInput").value;
var height = document.getElementById("heightInput").value;
var bmi = Math.round(weight / (height * height));
Math.round
written there and you might be thinking what is it for?function bmiCalc(weight, height) {
var bmi = Math.round(weight / (height * height));
return bmi;
}
Then I did the same as what I did early, imbedded the formula in a new variable with Math.round
var valueThree = document.getElementById("btn");
(SKIP THIS STEP IF YOU ALREADY KNOW ABOUT EVENT LISTENERS)
var button = document.querySelector('button');
button.addEventListener('click', function() {
alert('I got clicked!');
});
valueThree.addEventListener("click", function () {
var weight = document.getElementById("weightInput").value;
var height = document.getElementById("heightInput").value;
var bmi = bmiCalc(weight, height);
I covered the conditionals in my previous blog so check that out before reading the code below.
document.getElementById("result").innerHTML = "Your BMI is = " + bmi;
if (bmi < 18.5) {
document.getElementById("result").innerHTML =
"Your BMI is " + bmi + ", You are Underweight.";
} else if (bmi >= 18.5 && bmi <= 24.9) {
document.getElementById("result").innerHTML =
"Your BMI is " + bmi + ", You are in Normal weight. Keep Maintaining this.";
} else if (bmi >= 25 && bmi <= 29.9) {
document.getElementById("result").innerHTML =
"Your BMI is " +
bmi +
", You are Overweight. Try to do workout 2-3 days in week.";
} else if (bmi > 30) {
document.getElementById("result").innerHTML =
"Your BMI is " +
bmi +
", You are Obese. Try to do workout 5-6 days in week.";
}
});
Make sure to leave your important feedback, so that I can improve my future blogs.