• Tasks
  • task 125 • Body mass index

Hello there,
I've made the following code: (helper.js)

export const getBMICategory = (bmi) => {
 if (bmi < 18.6 ) {
      return 'Underweight';
  }

  if (bmi < 25 ) {
    return 'Normal weight';
  }

  if (bmi < 30 ) {
    return 'Overweight';
  }

  if (bmi > 29.99 ) {
    return 'Obesity';
  }
}

I haven't touch this: (solution.js)

/**
 * - **Underweight** = 18.5 or less
 * - **Normal weight** = greater than 18.5 and less than 25
 * - **Overweight** = starting at 25 and less than 30
 * - **Obesity** = 30 or greater
 **/

 import { getBMICategory } from './helper.js';

 console.log(getBMICategory(10));    // Underweight
 console.log(getBMICategory(20));    // Normal weight
 console.log(getBMICategory(40));    // Obesity

When I press the button Run your code I get the right results, and when pressing the button Submit task I will probably get a Status DONE.
I wonder if what I made is good practice code when using all these if statements or is there a better way to do this?

I just wanted to leave here my solution: (helper.js)

export const getBMICategory = (bmi) => {
  if (bmi <= 18.5 ) {
      return 'Underweight';
  }

  if (bmi < 25 ) {
    return 'Normal weight';
  }

  if (bmi < 30 ) {
    return 'Overweight';
  }

  if (bmi >= 30 ) {
    return 'Obesity';
  }
}

The same result could also be achieved using: ||
Ex.:

export const getBMICategory = (bmi) => {
  if (bmi < 18.5 || bmi = 18.5) {
      return 'Underweight';
  }

  if (bmi < 25 ) {
    return 'Normal weight';
  }

  if (bmi < 30 ) {
    return 'Overweight';
  }

  if (bmi > 30 || bmi = 30) {
    return 'Obesity';
  }
}

Thank you 😃
Oh! Sorry. You're right! Delete if you see fit, please.

9 months later

Hey team,
My helper.js code looks like so:

    if (bmi <= 18.5) {
        return 'Underweight';
    }
    if (bmi > 18.5 && bmi < 25) {
        return 'Normal Weight';
    }
    if (bmi >= 25 && bmi < 30) {
        return 'Overweight';
    }
    if (bmi >= 30) {
        return 'Obesity';
    }

}

And when I run my code, I get the desired results in the console:
Underweight
Normal Weight
Obesity

But when I submit the code, I get this error:

What could I be doing wrong?

    a month later
    4 months later

    petrusdrs regarding this particular task, you can do it with plain if statements or you can use if/else if you prefer.

    9 months later

    Please someone help me out here so my passion doesn't dry out.

    here is my code, giving the desired output but I keep getting error messages from the checker:

    // Implement the function getBMICategory(bmi) in helper.js. It should accept a single parameter bmi and

    // return the string representation of the body mass index category based on the following rules:

    // Underweight = 18.5 or less
    // Normal weight = greater than 18.5 and less than 25
    // Overweight = starting at 25 and less than 30
    // Obesity = 30 or greater

    export const getBMICategory = (bmi) => {
    if(bmi <= 18.5) {
    return 'Underweight';
    }
    if(bmi > 18.5 && bmi < 25) {
    return 'Normal weight';
    }
    if(bmi === 25 && bmi < 30) {
    return 'Overweight';
    }
    if(bmi >= 30) {
    return 'Obesity';
    }
    }

    Write a Reply...