BMI calculation challenge. Can anyone tell me why my codes aren't working?

form = document.querySelector("#calculate")

form.addEventListener('click',(e) {
  e.preventDefault()

  var height = parseFloat(document.getElementById('#heightInput').value)
  var weight = parseFloat(document.getElementById('#weightInput').value)
  var bmi = weight/(height*height)

  console.log(bmi);
  var b = document.querySelector(".blan button");
  b.innerHTML=bmi

  if(isNaN(weight) || isNaN(height) || weight <= 0 || height <= 0){
    alert("please enter valid information")
    b.innerHTML = "Please enter your weight and height you'll see your BMI.";
  }

  if(bmi < 18.5){
    var result = document.querySelector(".blan button");
    result.innerHTML="Underweight: Your BMI is less than 18.5.";
  }

  else if(BMI >= 18.5 && BMI < 25  ){
    var result= document.querySelector(".blan button");
    result.innerHTML= "Normalweight: Your BMI is ranging between 18.5 to 25. ";
  

  } else if (bmi >= 25 && bmi < 30) {
    var result= document.querySelector(".blan button");
    result.innerHTML= "Overweight: Your BMI is ranging between 25 to 30";
   
  } else if (bmi >= 30) {
    var result= document.querySelector(".blan button");
    result.innerHTML= "Obesse: Your BMI 30 or higher";
  }
  else {
  }
   return bmi.toFixed(2)
  }

)

Please post the HTML as well. Or better yet put it on Codepen so we have a live example to look at.

I’m guessing you don’t want to include the hash here. getElementById does not take a CSS selector as an argument. It takes the value of the id attribute.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.