1

I am trying to code a simple BMI calculator that takes in the Height & Weight of a person and gives BMI in a round figure. The formula I used is BMI = (weight)/(height * height)

My issue here is that without initializing the wt and ht variables my code is working fine. This means that

wt = prompt ("Enter your weight");
                ht = prompt ("Enter your height");

is working fine without initializing the wt & ht variables with var.

Can someone solve this silly doubt of mine, I would be grateful.

wt = prompt("Enter your weight");
ht = prompt("Enter your Height");

function bmi(wt, ht) {
  var bmiCal = (wt) / (ht * ht);
  bmiCal = Math.round(bmiCal);
  if (bmiCal < 18.5) {
    alert("Your BMI is " + bmiCal + "so you are underweight");
  }
  if (bmiCal >= 18.5 && bmiCal <= 24.9) {
    alert("Your BMI is " + bmiCal + "so you are normal");
  }
  if (bmiCal > 24.9) {
    alert("Your BMI is  " + bmiCal + " so you are overweight");
  }
}
bmi();
Ronith9
  • 11
  • 2
  • Possible duplicate of [What is the purpose of the var keyword and when should I use it (or omit it)?](https://stackoverflow.com/questions/1470488/what-is-the-purpose-of-the-var-keyword-and-when-should-i-use-it-or-omit-it) – VLAZ Oct 06 '19 at 18:03

2 Answers2

1

Update: If you don't use var you are basically creating a global variable. What is the purpose of the var keyword and when should I use it (or omit it)?

The problem here is the scope of the variables

 wt = prompt("Enter your weight"); //global wt variable
 ht = prompt("Enter your Height"); //global ht variable
// function bmi(wt, ht) function creating local variables wt,ht and masking the variables of the global scope
function bmi() //this will work
{
var bmiCal = (wt)/(ht * ht);
bmiCal = Math.round(bmiCal);
if (bmiCal<18.5)
{
    alert("Your BMI is " + bmiCal + "so you are underweight");
}
if (bmiCal >=18.5 && bmiCal <=24.9){
    alert("Your BMI is " + bmiCal + "so you are normal");
}
if (bmiCal >24.9){
    alert("Your BMI is  " + bmiCal + " so you are overweight");
}
}
bmi();

A cleaner way to write the code would be

function bmi() {
  let weight = prompt("Enter your weight"); // use let/const or var to create variables and keep them enclosed if you do not want them globally
  let height = prompt("Enter your height"); // use proper variable names
  let bmi = Math.round(weight / (height * height));
  if(bmi < 18.5)
    alert(`Your BMI is ${bmi}, so you are underweight`); //using string templates to remove multiple concatenation
  else if(bmi >= 18.5 && bmi <=24.9) // using else can reduce comparisons
    alert(`Your BMI is ${bmi}, so you are normal`); //using string 
  else if(bmi > 24.9)
    alert(`Your BMI is ${bmi}, so you are overweight`);
}

bmi();
Dhananjai Pai
  • 5,914
  • 1
  • 10
  • 25
1

Like @Dhananjai Pai stated . Your parameters doesn't use the values of the global variables:

function bmi() {
  var wt = prompt("Enter your weight");
  var ht = prompt("Enter your Height");
  var bmiCal = (wt) / (ht * ht);
  bmiCal = Math.round(bmiCal);
  if (bmiCal < 18.5) {
    alert("Your BMI is " + bmiCal + "so you are underweight");
  }
  if (bmiCal >= 18.5 && bmiCal <= 24.9) {
    alert("Your BMI is " + bmiCal + "so you are normal");
  }
  if (bmiCal > 24.9) {
    alert("Your BMI is  " + bmiCal + " so you are overweight");
  }
}
bmi();

Es6 solution:

    function bmi() {
      const wt = prompt("Enter your weight");
      const ht = prompt("Enter your Height");
      const bmiCal = (wt) / (ht * ht);
      bmiCal = Math.round(bmiCal);
      if (bmiCal < 18.5) {
        alert("Your BMI is " + bmiCal + " so you are underweight");
      }
      if (bmiCal >= 18.5 && bmiCal <= 24.9) {
        alert("Your BMI is " + bmiCal + " so you are normal");
      }
      if (bmiCal > 24.9) {
        alert("Your BMI is  " + bmiCal + " so you are overweight");
      }
    }
    bmi();
Melchia
  • 22,578
  • 22
  • 103
  • 117