How to Create BMI Calculator using Javascript
Tags: html, javascript, webdev, beginners
Published: 2022-01-12 13:51:16

If you want to make BMI calculator using JavaScript then this tutorial will help you. Here I have shared a tutorial of a [BMI calculator using JavaScript](https://www.foolishdeveloper.com/2021/10/bmi-calculator-using-javascript.html). BMI (Body Mass Index) is used to calculate the amount of mass in our body.

First I created a simple box on the webpage. In this box I have created all the information like heading, display and input slider.

[**Watch its live demo**](https://www.foolishdeveloper.com/2021/10/bmi-calculator-using-javascript.html) to learn how it works. I first created a heading using the h1 tag and designed it with CSS. Then he made a display in which all the information can be seen. In this display you can see the BMI and its status. In other words, the amount of BMI is more or less it will be shown through a text.

## BMI Calculator using Javascript

Then I created two range sliders using the input function of HTML. The first slider to input weight and the second input slider to input your height. Weight will be input in kg and height will be input in cm. 

By inputting these two information, your BMI can be seen in the automatic display. I used HTML CSS and JavaScript to build it. Designed [Simple BMI Calculator with HTML and CSS]() and implemented by JavaScript.

### 1. Make a box and add a heading

First the basic structure of this BMI calculator was created. For this we have taken the help of HTML and CSS below. First I made a box and added a heading in it. HTML's h1 tag is used to create headings.

```html
<div class="container">
  <h1>BMI Calculator</h1>

</div>
```
I have designed the webpage with the help of CSS below. Here the background color of the webpage is blue.
```css
*,
*:before,
*:after{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}
body{
    height: 100vh;
    background: #046db9;
}
```
Now who made the box. Box `width: 400px` and height depends on the amount of content. I used white as the background color and box-shadow to make the box more attractive.

Then the headings are designed. The background of the heading has been used in Garo blue and the text color has been used in white. `Font-size: 23px` has been used for `text-align: center` to keep the text in the middle and to increase the size.
```css
.container{
    background-color: #ffffff;
    padding: 30px 30px;
    width: 400px;
    position: absolute;
    transform: translate(-50%,-50%);
    top: 50%;
    left: 50%;
    border-radius: 5px;
    font-family: 'Poppins',sans-serif;
    box-shadow: 25px 25px 30px rgba(0,0,0,0.15);
}
.container h1{
   background: #024b94;
   color: white;
   text-align: center;
   font-size: 23px;
   letter-spacing: 1px;
   margin-top: -30px;
   margin-left: -30px;
   margin-right: -30px;
   margin-bottom: 40px;
}
```

![Make a box and add a heading](https://blogger.googleusercontent.com/img/a/AVvXsEiXULq1qTcvmJAzonSV-cFGW3oNjkaAsEC-7-TqB7_7m6Ml3bePGny5Xad5YW3kr9zNPH05fhzm78risS8S6YcApaNgxRzLJU-mQLmntTnDIib_8qKj3Ldkja9kfVxmfFctTtBgTxcS-2fTPBbu8u6-4X-mU9RXwo7LSwfszeUjwR7vgd9YBt3Voh25lA=s16000)
### 2. Create BMI result viewing area
Now we have created a display that will show the result of the calculation and a text. There is no specific size of the display it will depend on the amount of content.
```html
<div class="display">
   <p id="result">20.0</p>
   <p id="category">Normal weight</p>
</div>
```
```css
.display{
box-shadow: 0 0 20px rgba(0,139,253,0.25);
margin-bottom: 60px;
}

#result{
    font-size: 30px;
    font-weight: 700;
    letter-spacing: 1px;
    text-align: center;
    color: #0be881;
}

#category{
    font-size: 18px;
    text-align: center;
    letter-spacing: 1px;
}
```
![Create BMI result viewing area](https://blogger.googleusercontent.com/img/a/AVvXsEiA6_cI9OvpQ5i72Bk4188avOjHJs3jcUZTOJVMTzVmUBN9aXd000zCZ4dNQcKrxxrCTDt2qPHKcs5646_jQkGj7kExkCDcuNPvJMzIJMtMj5XNwdWtRPVT7Lfq1MlcZT6uKZC7x71Qey2v0mo6vsne8nZx-dGd1bthpmFAw5bAefPfwrhDLGBDwojOsg=s16000)
### 3. Create range slider to input height and weight
Now it's time to create a range slider. Who created the first range slider using the HTML and CSS code below. I used `type = "range"` using HTML input to create slider. Here the minimum and maximum values ​​are determined. This range is for slider weight input.

```html
<div class="row">
   <input type="range" min="20" max="200" value="20" id="weight" oninput="calculate()">
   <span id="weight-val">20 kg</span>
</div>
```
I have designed some basics of slider with the help of CSS below. Here the slider is used `width: 70%` and `height: 3.5px`.
```css
.row{
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 40px;
}
.row span{
    font-weight: 500;
}
input[type="range"]{
    width: 70%;
    height: 3.5px;
    -webkit-appearance: none;
    appearance: none;
    background-color: #dcdcdc;
    border-radius: 3px;
    outline: none;
}
```
Now I have designed the thumbnail. Each range slider has a button that changes the value of the range. The following CSS code has been used to design this button. Border-radius is used to make this thumbnail `width: 15px`, `height: 15px` and round.
```css
input[type="range"]::-webkit-slider-thumb{
    -webkit-appearance: none;
    appearance: none;
    height: 15px;
    width: 15px;
    background-color: #1c1c1c;
    border-radius: 50%;
    cursor: pointer;
}
```
Now I have made another slide to make a hairy anklet. It is made like this sea. Here minimum value 100 and maximum L250 are used. Flight No need to use any different code to design it because I have already added code to design here.
```html
<div class="row">
  <input type="range" min="100" max="250" value="100" id="height" oninput="calculate()">
  <span id="height-val">100 cm</span>
</div>
```
![Create range slider to input height and weight](https://blogger.googleusercontent.com/img/a/AVvXsEgXvK9pD1rXdBWoZmEk07bBK_4rB0ybX4ANdpylMUHNXAU-mA3d00WnPY8NcheaLMWAIGUEWKB0-ZOPRbPF1vzSL5A3HHq6N3H-hF2ZT-bMJZdRVbpNdCchFvzUB54yCo7JI-KnzGVaVydPlucpGf8LEffGODpSRyugPAFV2WtRLfSMF26VD09Ae_inyA=s16000)
### 4. JavaScript of BMI Calculator

Above we have designed this [JavaScript BMI Calculator](https://www.foolishdeveloper.com/2021/10/bmi-calculator-using-javascript.html) complete with the help of HTML and CSS. However, it is not yet effective. You need JavaScript enabled to view it. 

Below I have given all the JavaScript together and given the necessary explanations. Hopefully the following explanation will help you understand these JavaScripts.
```javascript
function calculate(){
  //Need to determine the constant of some id functions.
    var bmi;
    var result = document.getElementById("result");
  //The value of the height slider
    var height = parseInt(document.getElementById("height").value);
  //The value of the weight slider
    var weight = parseInt(document.getElementById("weight").value);
  
  //The value of height and width should be displayed in the webpage using "textContent".
    document.getElementById("weight-val").textContent = weight + " kg";
    document.getElementById("height-val").textContent = height + " cm";
  
//Now I have added the formula for calculating BMI in "bmi"
    bmi = (weight / Math.pow( (height/100), 2 )).toFixed(1);
  //With the help of "textContent" we have arranged to display in the result page of BMI
    result.textContent = bmi;

  
  //Now we have to make arrangements to show the text
  
  
  //When the BMI is less than 18.5, you can see the text below
    if(bmi < 18.5){
        category = "Underweight 😒";
        result.style.color = "#ffc44d";
    }
  
  //If BMI is >=18.5 and <=24.9
    else if( bmi >= 18.5 && bmi <= 24.9 ){
        category = "Normal Weight 😍";
        result.style.color = "#0be881";
    }
  
  //If BMI is >= 25 and <= 29.9 
    else if( bmi >= 25 && bmi <= 29.9 ){
        category = "Overweight 😮";
        result.style.color = "#ff884d";
    }
  
  //If BMI is <= 30
    else{
        category = "Obese 😱";
        result.style.color = "#ff5e57";
    }
  //All of the above text is stored in "category".

//Now you have to make arrangements to display the information in the webpage with the help of "textContent"
    document.getElementById("category").textContent = category;
}
```
![JavaScript of BMI Calculator](https://blogger.googleusercontent.com/img/a/AVvXsEg045ozdq4agA84VQACpObyoVHrVLdhZASMgLvT1Xw3JoV1LFcQPBO1jxmfGy20Qxetbg2yxhtK0ropCfXAYzZixCiK4Fqutn9TidF_Ok3MvXpgdhVcJXaviEZQW-Yp6E7-1zGyZdCa7O9ZMypRNnGMMZWdc3S-Jg8LzubkHlI41k_VxbgO3h0oCY5tgQ=s16000)
**Related Post:**
1. [Simple Weather App using JavaScript](https://www.foolishdeveloper.com/2021/12/simple-weather-app-using-javascript.html) 
2. [Make a Todo List using JavaScript](https://www.foolishdeveloper.com/2021/11/todo-list-javascript.html)
3. [Simple Stopwatch using JavaScript](https://www.foolishdeveloper.com/2021/10/simple-stopwatch-using-javascript.html)
4. [Skeleton Screen Loading Animation](https://www.foolishdeveloper.com/2021/09/skeleton-screen-loading-animation.html)
5. [Javascript Age Calculator](https://www.foolishdeveloper.com/2021/07/javascript-age-calculator.html)
6. [Random Password Generator with JavaScript](https://www.foolishdeveloper.com/2021/07/random-password-generator-javascript.html)
7. [Automatic Image Slider in Html, CSS](https://www.foolishdeveloper.com/2021/07/automatic-image-slider.html) 
8. [Sidebar Menu Using HTML CSS](https://www.foolishdeveloper.com/2021/06/sidebar-menu-using-html-css-javascript.html) 

Hopefully with the help of the above tutorial you have learned how I created this BMI calculator using HTML CSS and JavaScript. If there is any difficulty then you can let me know by commenting.

You can use this link if you want to [download](https://www.foolishdeveloper.com/2021/10/bmi-calculator-using-javascript.html) the required source code.

You can visit my blog for more tutorials like this. 😊
https://www.foolishdeveloper.com/