Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Uncaught Reference Error: (function) is not defined JAVASCRIPT


const markProperties = {
    fullName: 'Mark Miller',
    mass: 78,
    height: 1.69,
        calcBMI: function () {
        return this.mass / this.height ** 2;
    },
    bmi: calcBMI()



    


}

const johnProperties = {
    fullName: 'John Smith',
    mass: 92,
    height: 1.95,

    calcBMI: function () {
        this.bmi = this.mass / this.height ** 2;
        return this.bmi;
    },
    bmi: calcBMI()
};

const checkWinner = () => {
    if (johnProperties.bmi > markProperties.bmi) {
        return "John's BMI (" + johnProperties.bmi + ") is higher than Mark's BMI (" + markProperties.bmi + ")";
    } else if (markProperties.bmi > johnProperties.bmi) {
        return "Mark's BMI (" + markProperties.bmi + ") is higher than John's BMI (" + johnProperties.bmi + ")";
    }
}
console.log(checkWinner());

This is the code and it says that the function inside both objects is not defined. As I said, It brings an error that reads: error: Uncaught ReferenceError: calcBMI is not defined

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When you defining an object, you can’t execute a function that is being defined in the object.
In your case you should simply set a getter for bmi property instead:

const markProperties = {
    fullName: 'Mark Miller',
    mass: 78,
    height: 1.69,

    get bmi() {
        return this.mass / this.height ** 2;
    }
}

const johnProperties = {
    fullName: 'John Smith',
    mass: 92,
    height: 1.95,

    get bmi() {
        return this.mass / this.height ** 2;
    }
};

const checkWinner = () => {
    if (johnProperties.bmi > markProperties.bmi) {
        return "John's BMI (" + johnProperties.bmi + ") is higher than Mark's BMI (" + markProperties.bmi + ")";
    } else if (markProperties.bmi > johnProperties.bmi) {
        return "Mark's BMI (" + markProperties.bmi + ") is higher than John's BMI (" + johnProperties.bmi + ")";
    }
}
console.log(checkWinner());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading