Let's remember the first coding challenge where Mark and John compared their BMIs. Let's now implement the same functionality with objects and methods.
1. For each of them, create an object with properties for their full name, mass, and height
2. Then, add a method to each object to calculate the BMI. Save the BMI to the object and also return it from the method.
3. In the end, log to the console who has the highest BMI, together with the full name and the respective BMI. Don't forget they might have the same BMI.
Remember: BMI = mass / height^2 = mass / (height * height). (mass in kg and height in meter).
GOOD LUCK 😀
*/
// Solution:
varjohn={
fullName: 'John Smith',
mass: 110,
height: 1.95,
calcBMI: function(){
this.bmi=this.mass/(this.height*this.height);
returnthis.bmi;// 28.928336620644316
}
}
varmark={
fullName: 'Mark Miller',
mass: 78,
height: 1.69,
calcBMI: function(){
this.bmi=this.mass/(this.height*this.height);
returnthis.bmi;// 27.309968138370508
}
}
if(john.calcBMI()>mark.calcBMI()){
console.log(john.fullName+' has a higher BMI of '+john.bmi);// John Smith has a higher BMI of 28.928336620644316
}elseif(mark.bmi>john.bmi){
console.log(mark.fullName+' has a higher BMI of '+mark.bmi);