Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8e4d0a1

Browse files
src: Add solution to Coding Challenge 4
1 parent 3075714 commit 8e4d0a1

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

‎src/challenges/from-tutorial-1/3/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
// -----------------------------------------------------------------------------
3-
// Coding Challenge 2
3+
// Coding Challenge 3
44
// -----------------------------------------------------------------------------
55

66
/*
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>JavaScript Tutorials</title>
6+
</head>
7+
8+
<body>
9+
<h1>Coding Challenge 4</h1>
10+
</body>
11+
12+
<script src="index.js"></script>
13+
</html>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
// -----------------------------------------------------------------------------
3+
// Coding Challenge 4
4+
// -----------------------------------------------------------------------------
5+
6+
/*
7+
Let's remember the first coding challenge where Mark and John compared their BMIs.
8+
Let's now implement the same functionality with objects and methods.
9+
10+
1. For each of them, create an object with properties for their full name, mass, and height
11+
2. Then, add a method to each object to calculate the BMI. Save the BMI to the object and
12+
also return it from the method.
13+
3. In the end, log to the console who has the highest BMI, together with the full name and
14+
the respective BMI. Don't forget they might have the same BMI.
15+
16+
Remember: BMI = mass / height^2 = mass / (height * height). (mass in kg and height in meter).
17+
18+
GOOD LUCK 😀
19+
*/
20+
21+
var john = {
22+
fullName: "John Smith",
23+
mass: 75,
24+
height: 1.92,
25+
calculateBMI: function() {
26+
this.bmi = this.mass / (this.height ** 2)
27+
return this.bmi;
28+
}
29+
}
30+
john.calculateBMI();
31+
console.dir(john);
32+
33+
var mark = {
34+
fullName: "Mark Jones",
35+
mass: 84,
36+
height: 2.15,
37+
calculateBMI: function() {
38+
this.bmi = this.mass / (this.height ** 2)
39+
return this.bmi;
40+
}
41+
}
42+
mark.calculateBMI();
43+
console.dir(mark)
44+
45+
if (john.bmi > mark.bmi) {
46+
console.log(`${john.fullName} has the higher BMI: ${john.bmi}`);
47+
} else if (mark.bmi > john.bmi) {
48+
console.log(`${mark.fullName} has the higher BMI: ${mark.bmi}`);
49+
} else {
50+
console.log("Both have the same BMI");
51+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /