Preface
The BMI index (Body Mass Index in English) is a commonly used standard for measuring the degree of weight and whether the human body is healthy. When we need to compare and analyze the health impact of a person's weight on people of different heights, the BMI value is a neutral and reliable indicator. This article will introduce how to implement this calculator with JavaScript, let’s take a look together below.
Without further ado, just upload the code
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>BMI calculator</title></head><head><script> var BMI={}; BMI.getBMI=function(a,b){ var bmi=b/((a/100)*(a/100)); return bmi; }; BMI.idealweight=function(a){ var x=(a-100)*0.9; return x; }; function Cal(form){ var a=eval(form.height.value); var b=eval(form.weight.value); var bmi=eval(form.BMI.value); var bmiValue =BMI.getBMI(a,b); BMI.disp_alert(bmiValue ); form.IW.value=BMI.idealweight(a); form.BMI.value=bmiValue ; } BMI.disp_alert = function(bmi){ if (bmi < 18.5) { alert("You are too light, eat more!"); } else if (bmi >= 18.5 && bmi < 25) { alert("Dear, your weight is normal, keep it up!"); } else if (bmi >= 25 && bmi< 30) { alert("Dear, you are too heavy, you are going to lose weight!"); } else { alert("Dear, you are really going to lose weight!"); } } }</script></head><body><form method=post>Your height(cm):<input type="text" name="height"><br> <br/> Your weight(kg):<input type="text" name="weight"><br> <br/> <br/> <input type="button" value="Start calculation" onclick="Cal(this.form)"> <br/> <br/> Your ideal weight:<input type="text" name="IW"><br/> <br/> Your BMI:<input type="text" name="BMI"></form></body></html>Summarize
The above is all the content of implementing the BMI calculator with Javascript. It’s very simple. Interested friends can practice it yourself. I hope it will be helpful to everyone using JavaScript.