Minifycode 2021-02-25 Viewed 1.7K times Javascript

In this article, you will learn how to create BMI calculator in javascript?

BMI- Body mass index is defined as the body mass divided by the square of the body height and is expressed in units of kg/m2, resulting from mass in kilograms and height in meters.

 

<html>
 <head>
<title>Body Mass Index Calculator(BMI)</title>
<script type="text/javascript">
    function calculateBMI()
    {
        var height=Number(document.getElementById("height").value);
        var heightunits=document.getElementById("heightunits").value;
        var weight=Number(document.getElementById("weight").value);
        var weightunits=document.getElementById("weightunits").value;


        if (heightunits=="inches") height/=39.3700787;
        if (weightunits=="pounds") weight/=2.20462;

        var BMI=weight/Math.pow(height,2);

     
        document.getElementById("output").innerText=Math.round(BMI*100)/100;

        var output =  Math.round(BMI*100)/100
        if (output<18.5)
        document.getElementById("comment").innerText = "Underweight";
      else   if (output>=18.5 && output<=25)
        document.getElementById("comment").innerText = "Normal";
     else   if (output>=25 && output<=30)
        document.getElementById("comment").innerText = "Obese";
     else   if (output>30)
        document.getElementById("comment").innerText = "Overweight";
       // document.getElementById("answer").value = output; 
    }
</script>
 </head>
 <body>
<h1>Body Mass Index Calculator(BMI)</h1>
<p>Enter your height: <input type="text" id="height"/>
    <select type="multiple" id="heightunits">
        <option value="meters" selected="selected">meters</option>
        <option value="inches">inches</option>
    </select>
     </p>
<p>Enter your weight: <input type="text" id="weight"/>
    <select type="multiple" id="weightunits">
        <option value="kilograms" selected="selected">kilograms</option>
        <option value="pounds">pounds</option>
    </select>
</p>
<input type="submit" value="calculateBMI" onclick="calculateBMI();">
<h1>Your BMI is: <span id="output">?</span></h1>

<h2>This means you are: <span id="comment"> ?</span> </h2> 
 </body>

How to create BMI calculator in javascript? BMI- Body mass index is defined as the body mass divided by the square of the body height and is expressed in units of kg/m2, resulting from mass in kilograms and height in meters.
minify code