JavaScript Code Error

Hi, I created a BMI Calculator using javascript. But when I put any numbers to the height and weight, the result always shows as “Obesity” or “Underweight”. Can you check and help me where is wrong. thank you.

function btn_OnClick() {

var height = parseInt(document.getElementById("height").value,10);
var weight = parseInt(document.getElementById("weight").value,10);
var result = parseFloat((weight / (height * height,10)));

if (result < 18) {
    document.getElementById("comment1").textContent = "Underweight";
}
if (result >= 18 &&  result <= 24) {
    document.getElementById("comment1").textContent = "Ideal weight";
}
if (result >= 25 && result <= 30) {
    document.getElementById("comment1").textContent = "Overweight";
}
if (result > 30) {
    document.getElementById("comment1").textContent = "Obesity";
}

document.getElementById("result").innerHTML = "Your bmi score is : " + result;

}

function myFunctionheight() {
var x = document.getElementById(“height”);
var validation = 0;

if (isNaN(x.value) || x.value == '' || x.value <= 0 || x.value > 6) {
    alert('Invalid height Number');
}
else {
    return true;
}

}

function myFunctionweight() {
var x = document.getElementById(“weight”);
var validation = 0;

    if (isNaN(x.value) || x.value == '' || x.value <= 0 || x.value > 500)
    {
        alert('Invalid weight Number');
    }
    else
    {
        return true;       
    }

}

It looks like your code block got a little butchered; not sure if it is due to Discourse or how you formatted it. It’s all good though.

You should start by looking at your result declaration:
var result = parseFloat((weight / (height * height,10)));

You’re using parenthesis to dictate the order of operations. But, look at how you’re closing everything out. The formula you’re working with is:

Weight (kg) / Height (m)^2

You’re on the right path to express that. But, you’re mixing the parseFloat function call and the parenthesis in your calculation.

1 Like

hey bhensley,

Thanks for the reply.

well I’m a little bit confused about that, can you show me how I do changes :slight_smile:

Thanks.

Look at the parenthesis, the innermost ones contain weight * weight, 10 which is not exactly what you want as this will end using just the 10, and parseFloat() has only one argument, as the 10 is inside the parenthesis

I would rather you break this down yourself, so you better understand the issue and learn to debug these instances. One thing you can do, to better visualize what’s going on, is split the line up.

var result = parseFloat(
  (
    weight / (
      height * height, 10
    )
  )
)

So you have the parseFloat function, which actually only has a single parameter defined. And that one parameter is an expression of:

( weight / (height * height, 10) )

You’re probably not meaning to issue parseFloat’s second parameter in this expression. But, you have, as you haven’t properly formatted your parenthesis. You need to look at where you’re closing out your expressions.

/// Wrong
const randomExpr = someFunc((a / (b * b, c)));

/// Right
const randomExpr = someFunc((a / (b * b)), c);