-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchallenge_4.js
56 lines (49 loc) · 1.81 KB
/
challenge_4.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
47
48
49
50
51
52
53
54
55
56
/********************************************************************************************
* CODING CHALLENGE 4
*/
/*
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 😀
*/
// John's Object
var john = {
fullName: "John Smith",
mass: 85, // kg
height: 1.8 // meters
};
console.log(john);
// Mike's Object
var mike = {
fullName: "Mike Smith",
mass: 90, // kg
height: 1.95 // meters
};
console.log(mike);
// John's BMI Calculator
john.calcBMI = function() {
this.bmi = this.mass / this.height ** 2;
return this.bmi;
}
// Mike's BMI Calculator
mike.calcBMI = function() {
this.bmi = this.mass / this.height ** 2;
return this.bmi;
}
var maxBMI = function() {
console.log("John's BMI = " + john.calcBMI());
console.log("Mike's BMI = " + mike.calcBMI());
if(john.bmi < mike.bmi)
console.log(mike.fullName + " has a higher BMI of " + mike.bmi);
else if(john.bmi > mike.bmi)
console.log(john.fullName + " has a higher BMI of " + john.bmi);
else console.log(
"Both " + john.fullName + " & " + mike.fullName + " have the same BMI = " +
john.bmi
);
}
// Calling the maxBMI() function which will generate required BMI values for both John & Mike
maxBMI();