In this blog;

In this blog, I focus mainly on the JavaScript part of this BMI Calculator.

<!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 &copy;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;
  }
}

Now, Let's move to the interesting part, which is JavaScript.

STEP-2

var valueTwo = document.getElementById("heightInput");

STEP-3

Now, after getting both the values and storing those values in our variables, it's time to handle the calculations.

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()

Let's see both methods and figure out which one works well for us.

Method-1:- by simply retrieving the values, storing them in variables, and completing the calculation

var weight = document.getElementById("weightInput").value;
var height = document.getElementById("heightInput").value;
var bmi = Math.round(weight / (height * height));

Method-2:- by using the function()

function bmiCalc(weight, height) {
  var bmi = Math.round(weight / (height * height));
  return bmi;
}

Here,

Que) WHICH METHOD SHOULD WE USE HERE?

Ans) The Function Method()

Now, it's time to make the BMI calculator more functional and write some more code for the ‘button’ element, so that when the user enters height and weight and clicks the button, BMI gets displayed on the screen.

STEP-4

Now, by this time I think you guys should know how we can access the element from the document.

var valueThree = document.getElementById("btn");

STEP-5

(SKIP THIS STEP IF YOU ALREADY KNOW ABOUT EVENT LISTENERS)

Que) WHAT IS EVENT LISTENER?

Ans) Event listener is a function that we attach to an HTML element and then it listens for the specific event to happen, be it a mouse click, or keyboard input, or anything else.

It takes two arguments;

  1. first is the ‘event’ that is happening, in this case, click event

  2. function block, to execute a specific block of code that we want to work when the click happens.

var button = document.querySelector('button');
button.addEventListener('click', function() {
  alert('I got clicked!');
});

STEP-6

valueThree.addEventListener("click", function () {
  var weight = document.getElementById("weightInput").value;
  var height = document.getElementById("heightInput").value;

  var bmi = bmiCalc(weight, height);

STEP-7

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.";
}
});

That's it for this blog!

I tried to explain this in the easiest way possible, so if you like the blog and if you think you learn something new today, make sure to leave a like and comment.

Make sure to leave your important feedback, so that I can improve my future blogs.


Also published here.