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
CodePudding user response:
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());