简体   繁体   中英

Why i am getting Nan

Trying to calculate BMI using the following code:

var mark = {
  fullName: 'John Cartor',
     Weight : 90,
     height : 1.59,
     calcBMI: function(){
        this.bmi= this.weight / (this.height* this.height);
       return this.bmi;
     }
};

john.calcBMI();
mark.calcBMI();

console.log(john, mark);

But I'm getting Not a number error.

I'm not sure what youre trying to achieve but you can try this:

 var mark = { fullName: 'John Cartor', weight: 90, height: 1.59, calcBMI: function(){ this.bmi= this.weight / (this.height* this.height); return this.bmi; } }; console.log(mark.calcBMI());

Property Weight should be weight and then you can call the method calcBMI() .

I'm showing you this approach as you've hard coded all the values. So, in this case it is pretty straight forward.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM