Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
And here goes your next coding challenge. 2
2
This time number three, 3
3
for you to review all the material about objects. 4
4
And I hope that you're really doing 5
5
all these coding challenges. 6
6
Because as I mentioned earlier, 7
7
they are really, really important for your progress. 8
8
And I designed them all very carefully 9
9
to exactly match the progress of the course 10
10
and place them in strategic points of the curriculum. 11
11
So I cannot stress enough how important it is 12
12
that you do these challenges even if it's a bit difficult 13
13
but that's why they are called challenges. 14
14
They are really to challenge you and to make a thing 15
15
and if necessary spent half an hour or one hour 16
16
even to really try to figure out how the code works 17
17
without coding on your own, 18
18
it's impossible to learn programming, believe me. 19
19
But anyway, let's now get to the challenge. 20
20
And in this one, we're going back to Mark and John comparing 21
21
their body mass indexes. 22
22
And this time we're gonna use objects 23
23
to implement these calculations. 24
24
So here we have the formula again to calculate the BMI. 25
25
And so your tasks are, first for each of them, 26
26
so for Mark and John create an object with properties 27
27
for their full name, their mass and their height. 28
28
So their full names are actually Mark Miller and John Smith. 29
29
Then create a method called calcBMI on each object 30
30
which will then calculate the BMI based on the data 31
31
that is on the object. 32
32
So create the both method and add the exact same method 33
33
to both objects. 34
34
So write this method first in one of the objects 35
35
and then copy the exact same method 36
36
without changing it to the other object. 37
37
And this method should store the BMI value 38
38
to a property and it should also return it from the method. 39
39
So a little bit like the cogH method 40
40
that we did in the previous lecture 41
41
then lock to the console who of the two has to hire a BMI 42
42
and this string should look like this. 43
43
So it should include the full name 44
44
and actually here it's not the full name 45
45
but do include the full name that's why we have it. 46
46
So John Smith BMI is higher than Mark Miller's. 47
47
So not killers, but Mark Millers 48
48
and then also include the actual BMI value here. 49
49
And here is the test data 50
50
and I believe it's the same data as before. 51
51
So good luck with this challenge. 52
52
And again, it might be challenging 53
53
but please give it a try anyway, okay. 54
54
So I see you here in a minute 55
55
or maybe in half an hour, who knows? 56
56
It doesn't matter how long you take. 57
57
Now, okay, so let's get to work. 58
58
So number one is creating an object for each of them. 59
59
So object Mark. 60
60
So full name property, Mark Miller, 61
61
then the mass which is 78 kilograms 62
62
and then the height which is 1.69 meters. 63
63
So that's it for now. 64
64
And now I'm just gonna copy this, 65
65
as always, I'm a bit lazy 66
66
and that's just how programmers are, I guess. 67
67
So John Smith and he is 92 kilos 68
68
and 1.95 meters tall. 69
69
Okay, so that is point number one. 70
70
And now we will create the cog BMI method. 71
71
And then a function without any parameters 72
72
because we will take the data directly from the object 73
73
just like we did it with the cog H method previously. 74
74
And remember that it says here that we should store 75
75
the BMI value to a property and also return it 76
76
from the method. 77
77
So this means that we will do this .BMI 78
78
and then do the calculation here. 79
79
And remember, from the formula to BMI is calculated 80
80
by this .mass divided by this .height squared. 81
81
So exponentiation operator with two 82
82
and then also return it. 83
83
So at this .BMI 84
84
and just to see if this actually works, 85
85
let's actually call this method. 86
86
So mark.calc.BMI 87
87
and now I will quickly lock to the console, 88
88
the property that we just created. 89
89
So I could have locked this to the console directly 90
90
but I want to do it this way now. 91
91
And it doesn't really matter. 92
92
So with this, remember we created a property called BMI. 93
93
And so that is now of course accessible here. 94
94
So mark.BMI because remember that here in this method, 95
95
this now points to Mark because that is the object 96
96
that is calling the method 97
97
and therefore this .BMI will be on Mark. 98
98
So mark.BMI equals mark.mass divided by mark.height. 99
99
And so that's how we get this data into this method. 100
100
All right, so that's essentially what we learned 101
101
in the last lecture. 102
102
And so here I'm doing the same. 103
103
I'm first calculating this value 104
104
and then I'm doing mark.BMI 105
105
which was added here in this method call. 106
106
Of course without this method call, 107
107
mark.BMI would not be available 108
108
because this method here it doesn't call itself. 109
109
We need to explicitly call it. 110
110
Now, console are missing the .log. 111
111
How can that still happen after all these years? 112
112
Okay, so here we get the BMI of 27 which seems about right. 113
113
And now as I said, we will simply go ahead 114
114
and copy this method exactly the same to this John object 115
115
and it will work just the same, believe me. 116
116
So let me show that to you. 117
117
So now we call John.calcBMI. 118
118
And so now the object calling calcBMI is John. 119
119
And so now in this method here, 120
120
this will point to the John object. 121
121
And so this time John.BMI will be calculated 122
122
using John's data of mass and height. 123
123
Okay, are you still following that? 124
124
As always, you can pause the video, 125
125
analyze the code on your own 126
126
or you can always rewind to hear the explanation again. 127
127
I actually like to do that myself 128
128
when I watch other online video courses. 129
129
So I think that's a good tip maybe. 130
130
Anyway, so we just calculated the BMI of John here as well 131
131
and so now I'm logging both to the console. 132
132
And so yeah, this one also looks correct. 133
133
And so this means that simply copying this method 134
134
from one place to the other actually makes it work 135
135
and adapt to the data of each objects. 136
136
Now having the exact same code in both of these objects, 137
137
kind of violates the don't repeat yourself principle, right. 138
138
And actually there is a way in which we can avoid this. 139
139
And of course we will learn how 140
140
at a later point in the course 141
141
using something called object oriented programming. 142
142
But for now we are still learning the basics 143
143
and just want to make things work. 144
144
And of course have some fun along the way. 145
145
So this was the hardest part I believe 146
146
at least now all we have to do is to write this string here. 147
147
Let me actually copy the examples 148
148
so we have it as a reference down here. 149
149
Otherwise I'm gonna get a bit lost. 150
150
So if Mark.BMI is larger than John.BMI, then console.log. 151
151
So let's get the full name now from the object. 152
152
So mark.fullName BMI and now we also need it here 153
153
in these parenthesis. 154
154
So let's put it there, Mark.BMI is higher than 155
155
and I'll let me just copy it here. 156
156
So and again, I'm using command D 157
157
to select the next occurrence 158
158
and then let's just replace it with John. 159
159
So Mark's BMI is higher than John's BMI. 160
160
And now let's grab this again. 161
161
Else if John BMI is larger than Mark.BMI 162
162
then essentially say the opposite. 163
163
So here it's gonna be John and again command D 164
164
and here it's gonna be Mark, okay. 165
165
So 27 is higher than 24 and that 27 belongs to Mark. 166
166
And so this seems to be about right 167
167
but it's not completely right. 168
168
So of course here it should be lowercase. 169
169
So BMI and now I cannot use the command D 170
170
because then it will select this one as well. 171
171
So I'm double clicking on this one 172
172
and I hope that you're finding 173
173
these VS code tips useful as well. 174
174
So I'm clicking or actually I'm double clicking on this one, 175
175
Then I hold down the command key 176
176
which in your case might be the control key 177
177
or maybe the option key. 178
178
So I know there are different configurations for this 179
179
and it might not even work for you. 180
180
So please don't ask any question in that case 181
181
but for me, I just have to hit command 182
182
and then I can double click on multiple words 183
183
and then all of them get selected. 184
184
So now I have these four multiple cursors here again 185
185
and I can simply delete all of them 186
186
And right the one that I'm interested in. 187
187
so that's the lowercase BMI 188
188
and now we get to correct values 189
189
And also this conditional logic is correct as well. 190
190
And so it is we did actually solve the challenge 191
191
and with this we also finished this introduction 192
192
to JavaScript objects. 193
193
And once again, I really want to congratulate you 194
194
for the progress that you're making here. 195
195
Great job and really well done. 196
196
And I really mean it because I know how hard it is 197
197
to stick to a course like this 198
198
and to learn a completely new thing 199
199
that is probably a bit confusing 200
200
for you at this point still. 201
201
Just keep in mind that that is absolutely no problem at all. 202
202
And actually in the next section I believe, 203
203
I have a video about how to learn, 204
204
how to learn basically so about learning how to code. 205
205
And there's a lot of great tips that I'm sharing there. 206
206
So once you reach that, make sure to watch it carefully. 207
207
But now anyway, I will leave you to review 208
208
this lecture if you want or to move on together with me 209
209
to the next video 210
210
where we're now gonna start talking about loops.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.