JavaScript - BMI Calculator Code

In this tutorial we will create a BMI Calculator Code using JavaScript. This code will calculate a person personal bmi when user enter a specific physical information. The code use a onclick() function to call a specific code that will calculate the given value by the use of a special formula weight/(height/100*height/100) This code is free to use, you can modify it as your own, We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, iniitial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a href="https://sourcecodester.com" class="navbar-brand">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - BMI Calculator Code</h3>
  15.                 <hr style="border-top:1px dotted #ccc;" />
  16.                 <form>
  17.                         <div class="form-inline">
  18.                                 <label>Weight(kg):</label>
  19.                                 <input type="text" class="form-control" id="weight" size="10" >
  20.                         </div>
  21.                         <div class="form-inline">
  22.                                 <label>Height(cm):</label>
  23.                                 <input type="text" class="form-control" id="height" size="10">
  24.                         </div>
  25.                         <br />
  26.                         <button class="btn btn-primary" type="button" onClick="calculateBmi()">Calculate</button>
  27.                         <br /><br />
  28.                         <div class="form-inline">
  29.                                 <label>BMI:</label>
  30.                                 <input class="form-control" type="text" id="bmi" size="10">
  31.                         </div>
  32.                         <br />
  33.                         <div class="form-inline">
  34.                                 <label>Result:</label>
  35.                                 <input class="form-control" type="text" id="result" size="25">
  36.                         </div>
  37.                         <br />
  38.                         <button class="btn btn-success" type="reset">Reset</button>
  39.                 </form>
  40.         </div>
  41. <script src="js/script.js"></script>
  42. </body>
  43. </html>

Creating the Script

This code contains the script of the application. This code will calculate a person BMI when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. function calculateBmi() {
  2.         var weight = document.getElementById('weight').value;
  3.         var height = document.getElementById('height').value;
  4.        
  5.         if(weight > 0 && height > 0){  
  6.                 var bmi = weight/(height/100*height/100);
  7.                 document.getElementById('bmi').value = bmi;
  8.                
  9.                 if(bmi < 18.5){
  10.                         document.getElementById('result').value = "You are too thin.";
  11.                 }
  12.                 if(bmi > 18.5 && bmi < 24.9){
  13.                         document.getElementById('result').value = "You are healthy.";
  14.                 }
  15.                 if(bmi > 25){
  16.                         document.getElementById('result').value = "You are overweight.";
  17.                 }
  18.         }
  19.         else{
  20.                 alert("Please enter something first!")
  21.         }
  22. }
There you have it we successfully created a BMI Calculator Code using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment