Home > Back-end >  Calculating body mass index with javascript
Calculating body mass index with javascript

Time:11-03

I want to write a code that calculates body mass index. But I'm having a problem with multiplications. When I look at the sites, it is done in the same way, but it did not work for me. I will be glad if you help me.

BMI = (weight, height) => {
  let bmi = weight / (height ** 2)
  if (bmi < 18.5) {
    console.log(`underweight`)
  } else if ((bmi => 18.5) && (bmi <= 24.9)) {
    console.log(`normal weight`)
  } else if ((bmi => 25) && (bmi <= 29.9)) {
    console.log(`overweight`)
  } else {
    console.log(`obese`)
  }
  console.log(bmi)
}

BMI(120, 181);

gives

0.003662891853118037
underweight

Normally, someone who is 120 kilos and 181 height should be obese with a body mass index of 36.6. But I have 0.00366 such a result and it comes out as weak.

CodePudding user response:

Make sure you input height in meters. If you wish to use centimeters, change the formula to let bmi =(weight / ((height * height) / 10000))

CodePudding user response:

change

let bmi = weight/(height**2)

to

let bmi = weight/((height/100)**2)

CodePudding user response:

It is because answer is in decimal form which is less than 18.5 Try this let bmi = (weight / ((height * height) / 10000)).toFixed(2); You formula is also correct but this one might help.

  • Related