Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
1
00:00:01,200 --> 00:00:04,430
And here goes your next coding challenge.
2
2
00:00:04,430 --> 00:00:06,180
This time number three,
3
3
00:00:06,180 --> 00:00:09,863
for you to review all the material about objects.
4
4
00:00:11,490 --> 00:00:13,320
And I hope that you're really doing
5
5
00:00:13,320 --> 00:00:15,340
all these coding challenges.
6
6
00:00:15,340 --> 00:00:17,310
Because as I mentioned earlier,
7
7
00:00:17,310 --> 00:00:20,900
they are really, really important for your progress.
8
8
00:00:20,900 --> 00:00:23,430
And I designed them all very carefully
9
9
00:00:23,430 --> 00:00:26,110
to exactly match the progress of the course
10
10
00:00:26,110 --> 00:00:30,200
and place them in strategic points of the curriculum.
11
11
00:00:30,200 --> 00:00:33,260
So I cannot stress enough how important it is
12
12
00:00:33,260 --> 00:00:37,130
that you do these challenges even if it's a bit difficult
13
13
00:00:37,130 --> 00:00:40,200
but that's why they are called challenges.
14
14
00:00:40,200 --> 00:00:43,580
They are really to challenge you and to make a thing
15
15
00:00:43,580 --> 00:00:47,060
and if necessary spent half an hour or one hour
16
16
00:00:47,060 --> 00:00:51,260
even to really try to figure out how the code works
17
17
00:00:51,260 --> 00:00:52,900
without coding on your own,
18
18
00:00:52,900 --> 00:00:55,823
it's impossible to learn programming, believe me.
19
19
00:00:56,690 --> 00:01:00,300
But anyway, let's now get to the challenge.
20
20
00:01:00,300 --> 00:01:04,620
And in this one, we're going back to Mark and John comparing
21
21
00:01:04,620 --> 00:01:07,020
their body mass indexes.
22
22
00:01:07,020 --> 00:01:09,400
And this time we're gonna use objects
23
23
00:01:09,400 --> 00:01:11,960
to implement these calculations.
24
24
00:01:11,960 --> 00:01:16,250
So here we have the formula again to calculate the BMI.
25
25
00:01:16,250 --> 00:01:21,030
And so your tasks are, first for each of them,
26
26
00:01:21,030 --> 00:01:25,250
so for Mark and John create an object with properties
27
27
00:01:25,250 --> 00:01:29,150
for their full name, their mass and their height.
28
28
00:01:29,150 --> 00:01:33,880
So their full names are actually Mark Miller and John Smith.
29
29
00:01:33,880 --> 00:01:38,880
Then create a method called calcBMI on each object
30
30
00:01:39,580 --> 00:01:43,150
which will then calculate the BMI based on the data
31
31
00:01:43,150 --> 00:01:45,080
that is on the object.
32
32
00:01:45,080 --> 00:01:48,890
So create the both method and add the exact same method
33
33
00:01:48,890 --> 00:01:50,600
to both objects.
34
34
00:01:50,600 --> 00:01:54,000
So write this method first in one of the objects
35
35
00:01:54,000 --> 00:01:56,160
and then copy the exact same method
36
36
00:01:56,160 --> 00:01:58,980
without changing it to the other object.
37
37
00:01:58,980 --> 00:02:01,820
And this method should store the BMI value
38
38
00:02:01,820 --> 00:02:06,160
to a property and it should also return it from the method.
39
39
00:02:06,160 --> 00:02:09,180
So a little bit like the cogH method
40
40
00:02:09,180 --> 00:02:11,070
that we did in the previous lecture
41
41
00:02:11,070 --> 00:02:14,970
then lock to the console who of the two has to hire a BMI
42
42
00:02:16,356 --> 00:02:19,760
and this string should look like this.
43
43
00:02:19,760 --> 00:02:22,760
So it should include the full name
44
44
00:02:22,760 --> 00:02:24,800
and actually here it's not the full name
45
45
00:02:24,800 --> 00:02:29,040
but do include the full name that's why we have it.
46
46
00:02:29,040 --> 00:02:34,040
So John Smith BMI is higher than Mark Miller's.
47
47
00:02:37,250 --> 00:02:41,300
So not killers, but Mark Millers
48
48
00:02:41,300 --> 00:02:45,450
and then also include the actual BMI value here.
49
49
00:02:45,450 --> 00:02:46,836
And here is the test data
50
50
00:02:46,836 --> 00:02:51,320
and I believe it's the same data as before.
51
51
00:02:51,320 --> 00:02:54,470
So good luck with this challenge.
52
52
00:02:54,470 --> 00:02:56,550
And again, it might be challenging
53
53
00:02:56,550 --> 00:03:00,320
but please give it a try anyway, okay.
54
54
00:03:00,320 --> 00:03:02,170
So I see you here in a minute
55
55
00:03:02,170 --> 00:03:05,110
or maybe in half an hour, who knows?
56
56
00:03:05,110 --> 00:03:06,933
It doesn't matter how long you take.
57
57
00:03:09,020 --> 00:03:12,610
Now, okay, so let's get to work.
58
58
00:03:12,610 --> 00:03:17,610
So number one is creating an object for each of them.
59
59
00:03:18,600 --> 00:03:20,593
So object Mark.
60
60
00:03:24,170 --> 00:03:28,763
So full name property, Mark Miller,
61
61
00:03:30,100 --> 00:03:34,730
then the mass which is 78 kilograms
62
62
00:03:35,820 --> 00:03:40,327
and then the height which is 1.69 meters.
63
63
00:03:42,360 --> 00:03:44,680
So that's it for now.
64
64
00:03:44,680 --> 00:03:47,743
And now I'm just gonna copy this,
65
65
00:03:48,930 --> 00:03:50,740
as always, I'm a bit lazy
66
66
00:03:50,740 --> 00:03:53,593
and that's just how programmers are, I guess.
67
67
00:03:54,860 --> 00:03:59,860
So John Smith and he is 92 kilos
68
68
00:04:02,700 --> 00:04:06,233
and 1.95 meters tall.
69
69
00:04:07,440 --> 00:04:10,980
Okay, so that is point number one.
70
70
00:04:10,980 --> 00:04:14,653
And now we will create the cog BMI method.
71
71
00:04:19,540 --> 00:04:22,870
And then a function without any parameters
72
72
00:04:22,870 --> 00:04:26,520
because we will take the data directly from the object
73
73
00:04:26,520 --> 00:04:30,670
just like we did it with the cog H method previously.
74
74
00:04:30,670 --> 00:04:33,800
And remember that it says here that we should store
75
75
00:04:33,800 --> 00:04:37,970
the BMI value to a property and also return it
76
76
00:04:37,970 --> 00:04:39,033
from the method.
77
77
00:04:40,330 --> 00:04:45,330
So this means that we will do this .BMI
78
78
00:04:45,590 --> 00:04:48,290
and then do the calculation here.
79
79
00:04:48,290 --> 00:04:51,730
And remember, from the formula to BMI is calculated
80
80
00:04:51,730 --> 00:04:56,730
by this .mass divided by this .height squared.
81
81
00:05:00,020 --> 00:05:03,430
So exponentiation operator with two
82
82
00:05:04,500 --> 00:05:06,980
and then also return it.
83
83
00:05:06,980 --> 00:05:08,480
So at this .BMI
84
84
00:05:11,050 --> 00:05:14,710
and just to see if this actually works,
85
85
00:05:14,710 --> 00:05:17,100
let's actually call this method.
86
86
00:05:17,100 --> 00:05:19,760
So mark.calc.BMI
87
87
00:05:21,110 --> 00:05:24,290
and now I will quickly lock to the console,
88
88
00:05:24,290 --> 00:05:26,680
the property that we just created.
89
89
00:05:26,680 --> 00:05:30,740
So I could have locked this to the console directly
90
90
00:05:30,740 --> 00:05:33,350
but I want to do it this way now.
91
91
00:05:33,350 --> 00:05:35,120
And it doesn't really matter.
92
92
00:05:35,120 --> 00:05:39,823
So with this, remember we created a property called BMI.
93
93
00:05:40,660 --> 00:05:43,340
And so that is now of course accessible here.
94
94
00:05:43,340 --> 00:05:48,340
So mark.BMI because remember that here in this method,
95
95
00:05:49,160 --> 00:05:53,970
this now points to Mark because that is the object
96
96
00:05:53,970 --> 00:05:55,800
that is calling the method
97
97
00:05:55,800 --> 00:06:00,270
and therefore this .BMI will be on Mark.
98
98
00:06:00,270 --> 00:06:05,270
So mark.BMI equals mark.mass divided by mark.height.
99
99
00:06:07,250 --> 00:06:11,053
And so that's how we get this data into this method.
100
100
00:06:13,350 --> 00:06:15,610
All right, so that's essentially what we learned
101
101
00:06:15,610 --> 00:06:17,090
in the last lecture.
102
102
00:06:17,090 --> 00:06:18,560
And so here I'm doing the same.
103
103
00:06:18,560 --> 00:06:20,670
I'm first calculating this value
104
104
00:06:20,670 --> 00:06:23,250
and then I'm doing mark.BMI
105
105
00:06:23,250 --> 00:06:26,740
which was added here in this method call.
106
106
00:06:26,740 --> 00:06:29,220
Of course without this method call,
107
107
00:06:29,220 --> 00:06:31,990
mark.BMI would not be available
108
108
00:06:31,990 --> 00:06:34,600
because this method here it doesn't call itself.
109
109
00:06:34,600 --> 00:06:36,343
We need to explicitly call it.
110
110
00:06:38,830 --> 00:06:42,113
Now, console are missing the .log.
111
111
00:06:43,230 --> 00:06:46,143
How can that still happen after all these years?
112
112
00:06:47,480 --> 00:06:52,480
Okay, so here we get the BMI of 27 which seems about right.
113
113
00:06:52,740 --> 00:06:55,700
And now as I said, we will simply go ahead
114
114
00:06:55,700 --> 00:07:00,700
and copy this method exactly the same to this John object
115
115
00:07:02,760 --> 00:07:06,480
and it will work just the same, believe me.
116
116
00:07:06,480 --> 00:07:08,143
So let me show that to you.
117
117
00:07:09,580 --> 00:07:14,103
So now we call John.calcBMI.
118
118
00:07:14,970 --> 00:07:19,360
And so now the object calling calcBMI is John.
119
119
00:07:19,360 --> 00:07:21,420
And so now in this method here,
120
120
00:07:21,420 --> 00:07:24,760
this will point to the John object.
121
121
00:07:24,760 --> 00:07:28,690
And so this time John.BMI will be calculated
122
122
00:07:28,690 --> 00:07:31,643
using John's data of mass and height.
123
123
00:07:32,810 --> 00:07:35,163
Okay, are you still following that?
124
124
00:07:36,040 --> 00:07:38,850
As always, you can pause the video,
125
125
00:07:38,850 --> 00:07:40,750
analyze the code on your own
126
126
00:07:40,750 --> 00:07:45,200
or you can always rewind to hear the explanation again.
127
127
00:07:45,200 --> 00:07:46,920
I actually like to do that myself
128
128
00:07:46,920 --> 00:07:49,723
when I watch other online video courses.
129
129
00:07:50,600 --> 00:07:53,363
So I think that's a good tip maybe.
130
130
00:07:54,800 --> 00:07:58,970
Anyway, so we just calculated the BMI of John here as well
131
131
00:07:58,970 --> 00:08:01,823
and so now I'm logging both to the console.
132
132
00:08:02,760 --> 00:08:06,130
And so yeah, this one also looks correct.
133
133
00:08:06,130 --> 00:08:09,780
And so this means that simply copying this method
134
134
00:08:09,780 --> 00:08:13,020
from one place to the other actually makes it work
135
135
00:08:13,020 --> 00:08:16,840
and adapt to the data of each objects.
136
136
00:08:16,840 --> 00:08:20,950
Now having the exact same code in both of these objects,
137
137
00:08:20,950 --> 00:08:25,820
kind of violates the don't repeat yourself principle, right.
138
138
00:08:25,820 --> 00:08:29,380
And actually there is a way in which we can avoid this.
139
139
00:08:29,380 --> 00:08:31,760
And of course we will learn how
140
140
00:08:31,760 --> 00:08:33,800
at a later point in the course
141
141
00:08:33,800 --> 00:08:37,320
using something called object oriented programming.
142
142
00:08:37,320 --> 00:08:39,590
But for now we are still learning the basics
143
143
00:08:39,590 --> 00:08:43,010
and just want to make things work.
144
144
00:08:43,010 --> 00:08:46,210
And of course have some fun along the way.
145
145
00:08:46,210 --> 00:08:49,190
So this was the hardest part I believe
146
146
00:08:49,190 --> 00:08:54,190
at least now all we have to do is to write this string here.
147
147
00:08:55,510 --> 00:08:56,840
Let me actually copy the examples
148
148
00:08:56,840 --> 00:08:59,633
so we have it as a reference down here.
149
149
00:09:00,600 --> 00:09:02,873
Otherwise I'm gonna get a bit lost.
150
150
00:09:05,370 --> 00:09:10,370
So if Mark.BMI is larger than John.BMI, then console.log.
151
151
00:09:21,640 --> 00:09:24,900
So let's get the full name now from the object.
152
152
00:09:24,900 --> 00:09:29,900
So mark.fullName BMI and now we also need it here
153
153
00:09:31,950 --> 00:09:33,690
in these parenthesis.
154
154
00:09:33,690 --> 00:09:37,570
So let's put it there, Mark.BMI is higher than
155
155
00:09:42,850 --> 00:09:45,093
and I'll let me just copy it here.
156
156
00:09:48,610 --> 00:09:51,610
So and again, I'm using command D
157
157
00:09:51,610 --> 00:09:53,830
to select the next occurrence
158
158
00:09:53,830 --> 00:09:57,310
and then let's just replace it with John.
159
159
00:09:57,310 --> 00:10:01,073
So Mark's BMI is higher than John's BMI.
160
160
00:10:02,920 --> 00:10:04,893
And now let's grab this again.
161
161
00:10:05,790 --> 00:10:10,790
Else if John BMI is larger than Mark.BMI
162
162
00:10:16,970 --> 00:10:19,483
then essentially say the opposite.
163
163
00:10:20,370 --> 00:10:25,370
So here it's gonna be John and again command D
164
164
00:10:27,220 --> 00:10:31,193
and here it's gonna be Mark, okay.
165
165
00:10:33,730 --> 00:10:38,730
So 27 is higher than 24 and that 27 belongs to Mark.
166
166
00:10:40,170 --> 00:10:44,270
And so this seems to be about right
167
167
00:10:44,270 --> 00:10:46,160
but it's not completely right.
168
168
00:10:46,160 --> 00:10:50,450
So of course here it should be lowercase.
169
169
00:10:50,450 --> 00:10:54,840
So BMI and now I cannot use the command D
170
170
00:10:54,840 --> 00:10:57,540
because then it will select this one as well.
171
171
00:10:57,540 --> 00:11:00,030
So I'm double clicking on this one
172
172
00:11:00,030 --> 00:11:01,250
and I hope that you're finding
173
173
00:11:01,250 --> 00:11:04,950
these VS code tips useful as well.
174
174
00:11:04,950 --> 00:11:09,040
So I'm clicking or actually I'm double clicking on this one,
175
175
00:11:09,040 --> 00:11:11,660
Then I hold down the command key
176
176
00:11:11,660 --> 00:11:14,080
which in your case might be the control key
177
177
00:11:14,080 --> 00:11:16,100
or maybe the option key.
178
178
00:11:16,100 --> 00:11:19,010
So I know there are different configurations for this
179
179
00:11:19,010 --> 00:11:20,900
and it might not even work for you.
180
180
00:11:20,900 --> 00:11:25,080
So please don't ask any question in that case
181
181
00:11:25,080 --> 00:11:27,530
but for me, I just have to hit command
182
182
00:11:27,530 --> 00:11:30,840
and then I can double click on multiple words
183
183
00:11:30,840 --> 00:11:32,990
and then all of them get selected.
184
184
00:11:32,990 --> 00:11:36,300
So now I have these four multiple cursors here again
185
185
00:11:36,300 --> 00:11:38,760
and I can simply delete all of them
186
186
00:11:38,760 --> 00:11:41,893
And right the one that I'm interested in.
187
187
00:11:43,200 --> 00:11:46,100
so that's the lowercase BMI
188
188
00:11:46,100 --> 00:11:48,630
and now we get to correct values
189
189
00:11:48,630 --> 00:11:53,630
And also this conditional logic is correct as well.
190
190
00:11:54,160 --> 00:11:58,090
And so it is we did actually solve the challenge
191
191
00:11:58,090 --> 00:12:00,910
and with this we also finished this introduction
192
192
00:12:00,910 --> 00:12:02,970
to JavaScript objects.
193
193
00:12:02,970 --> 00:12:06,200
And once again, I really want to congratulate you
194
194
00:12:06,200 --> 00:12:09,090
for the progress that you're making here.
195
195
00:12:09,090 --> 00:12:11,670
Great job and really well done.
196
196
00:12:11,670 --> 00:12:14,950
And I really mean it because I know how hard it is
197
197
00:12:14,950 --> 00:12:17,360
to stick to a course like this
198
198
00:12:17,360 --> 00:12:19,840
and to learn a completely new thing
199
199
00:12:19,840 --> 00:12:22,110
that is probably a bit confusing
200
200
00:12:22,110 --> 00:12:24,290
for you at this point still.
201
201
00:12:24,290 --> 00:12:28,790
Just keep in mind that that is absolutely no problem at all.
202
202
00:12:28,790 --> 00:12:31,940
And actually in the next section I believe,
203
203
00:12:31,940 --> 00:12:34,560
I have a video about how to learn,
204
204
00:12:34,560 --> 00:12:38,330
how to learn basically so about learning how to code.
205
205
00:12:38,330 --> 00:12:41,340
And there's a lot of great tips that I'm sharing there.
206
206
00:12:41,340 --> 00:12:45,360
So once you reach that, make sure to watch it carefully.
207
207
00:12:45,360 --> 00:12:48,020
But now anyway, I will leave you to review
208
208
00:12:48,020 --> 00:12:51,550
this lecture if you want or to move on together with me
209
209
00:12:51,550 --> 00:12:52,910
to the next video
210
210
00:12:52,910 --> 00:12:56,393
where we're now gonna start talking about loops.
18511
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.