skip to Main Content

I want a function which return the BMI. If my weight is 65 and my height is 1.8 so what will be my round BMI.

function bmiCalculator(weight, height) {
  var bmi = 65/(1.8*1.8);
  bmi = Math.round(bmi);
}

bmiCalculator(); 

I have tried so many time but I get 15 BMI and the expected is 20.
Can some body explain what is going behind the scene?

2

Answers


  1. BMI is calculated by weight in kg dividing by height in meters multiplied by height in meters

    const weight = 65;
    const height = 1.8
    const bmi = weight / Math.pow(height, 2);
    console.log(bmi); // 20.061728395061728
    console.log(parseInt(bmi)); // 20
    
    Login or Signup to reply.
  2. You’re on the right track, just make sure to use the function parameters and call it.

    Also, name your params appropriately.

    /**
     * Compute BMI and round the result to an integer.
     *
     * @param {number} weightInKg - Weight in kg
     * @param {number} heightInMeters - Height in meters
     * @returns {int} rounded BMI result
     */
    function bmiCalculator(weightInKg, heightInMeters) {
      return Math.round(weightInKg / (heightInMeters ** 2));
    }
    
    console.log(`BMI: ${bmiCalculator(65, 1.8)}`); // 20
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search