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;
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.