Skip to content

Instantly share code, notes, and snippets.

@kironroy
Created May 3, 2023 20:57
Show Gist options
  • Save kironroy/f84ea2acec42f8c52e6860fa8d8b18bc to your computer and use it in GitHub Desktop.
Save kironroy/f84ea2acec42f8c52e6860fa8d8b18bc to your computer and use it in GitHub Desktop.
JavaScript Objects
const johnObj = {
fullName: 'John Smith',
mass: 92,
height: 1.95,
calcBMI: function () {
this.bmi = this.mass / this.height ** 2;
return this.bmi;
},
getSummary: function() {
return `${this.firstName} ${this.lastName} has a BMI of ${this.calcBMI()}.`
}
}
// console.log(johnObj.getSummary());
const markObj = {
fullName: 'Mark Miller',
mass: 78,
height: 1.69,
calcBMI: function () {
this.bmi = this.mass / this.height ** 2;
return this.bmi;
},
getSummary: function() {
return `${this.firstName} ${this.lastName} has a BMI of ${this.calcBMI()}.`
}
}
// function calls
markObj.calcBMI()
johnObj.calcBMI()
if (markObj.bmi > johnObj.bmi) {
console.log(`${markObj.fullName} has a BMI of ${markObj.bmi} which higher than ${johnObj.fullName}'s BMI: ${johnObj.bmi}`);
} else if (johnObj.bmi > markObj.bmi) {
console.log(`${johnObj.fullName} has a BMI of ${johnObj.bmi} which is more than ${markObj.fullName}'s BMI: ${markObj.bmi} `);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment