-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCodingChallenge4.js
46 lines (38 loc) · 1.45 KB
/
CodingChallenge4.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*****************************
* CODING CHALLENGE 4
*/
/*
Challenge Description =>
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:
var john = {
fullName: 'John Smith',
mass: 110,
height: 1.95,
calcBMI: function () {
this.bmi = this.mass / (this.height * this.height);
return this.bmi; // 28.928336620644316
}
}
var mark = {
fullName: 'Mark Miller',
mass: 78,
height: 1.69,
calcBMI: function () {
this.bmi = this.mass / (this.height * this.height);
return this.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
} else if (mark.bmi > john.bmi) {
console.log(mark.fullName + ' has a higher BMI of ' + mark.bmi);
} else {
console.log('They have the same BMI');
}