0
var weightkg=document.getElementById('weight').value;

    var heightinm=document.getElementById('height').value;
    
    function bmival (weightkg,heightinm){

        var hout=heightinm*2
        var output=weightkg/hout
        //make a full number
             // var r= Math.trunc(o)
             
         if (output<=18.5){
            return document.getElementById('print').innerHTML=`Your BMI is ${output} you are underweight` ;
         }
 
         else if(output>18.5 && o<25.5){
            return document.getElementById('print').innerHTML=`Your BMI is ${output} You come under fit catogery`;
         }
 
         else{
            return document.getElementById('print').innerHTML=`Your BMI is ${output} you are overweight and obese`;
         }
     }

[i am making a bmi cal that take input from user but i am getting a error and don't know what i am doing wrong]

** this is js code and when i run i get a NaN instead of Number **

4
  • How are you calling this code? You need to get the values of the inputs when they click the button to calculate, not when the page starts. Commented Aug 29, 2022 at 16:45
  • The global variable names weightkg and heightinm are the same as your function argument names, which is really confusing. Commented Aug 29, 2022 at 16:47
  • Does this answer your question? HTML input type="number" still returning a string when accessed from javascript Commented Aug 29, 2022 at 16:50
  • You will need to show the relevant part of your HTML. Commented Aug 30, 2022 at 11:44

2 Answers 2

1

The values from an input field are strings so you must convert them into numbers


var heightinm = +document.getElementById('height').value; // one way to do it.

Sign up to request clarification or add additional context in comments.

Comments

0

The value you get from

var weightkg=document.getElementById('weight').value; 

seems to be a string instead of number. You need to convert values to do Math operations on them. NaN tells you output is not a number. Turn your values to number with parseInt method.

var weightkg = parseInt(document.getElementById('weight').value); 

or you can just put a "+" to convert a string into number

var weightkg = +document.getElementById('weight').value; 

For Example.

const input = document.getElementById('input');
const defaultStringInput = document.getElementById('input').value;
const inputConverted = parseInt(document.getElementById('input').value);
const inputConverted2 = +document.getElementById('input').value;
input.addEventListener('change', () => {

  console.log(typeof defaultStringInput); // string
  console.log(typeof inputConverted); // number
  console.log(typeof inputConverted2); // number
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.