samedi 3 octobre 2020

I don't understand why this javascript code doesn't work (this statement) [closed]

I am currently learning javascript and want to know what is the problem here. I am following a course and the code I wrote is for a coding chalange.

var jhon = {
    firstName: 'Jhon',
    lastName: 'Smith',
    mass: 82,
    height: 1.73,
    calcBMI: function() {
        this.bmi = this.mass / (this.height * this.height);
    }
};

var mark = {
    firstName: 'Mark',
    lastName: 'Musk',
    mass: 72,
    height: 1.67,
    calcBMI: function() {
        this.bmi = this.mass / (this.height * this.height); 
    }
};

jhon.calcBMI;
mark.calcBMI;


if (jhon.bmi > mark.bmi) {
    console.log(jhon.firstName + " " + jhon.lastName + "'s BMI is bigger. BMI: " + jhon.bmi);
} else if (jhon.bmi < mark.bmi) {
    console.log(mark.firstName + " " + mark.lastName + "'s BMI is bigger. BMI: " + mark.bmi);
} else {
    console.log("Their BMI is the same (" + jhon.bmi + ").");
};

As you can see I'm using the this keyword for adding a new property into the jhon and mark objects called bmi. My browser prints out this to the console. Image of my console

I originally thought that it was a problem with the if statements but when I tried to list the bmi property in the console I was dumbfounded. Image of my console

I just don't know what to do so please help me if you can. Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire