Presenter: And here goes your next coding challenge.
This time number three,
for you to review all the material about objects.
And I hope that you're really doing
all these coding challenges.
Because as I mentioned earlier,
they are really, really important for your progress.
And I designed them all very carefully
to exactly match the progress of the course
and place them in strategic points of the curriculum.
So I cannot stress enough how important it is
that you do these challenges even if it's a bit difficult
but that's why they are called challenges.
They are really to challenge you and to make a thing
and if necessary spent half an hour or one hour
even to really try to figure out how the code works
without coding on your own,
it's impossible to learn programming, believe me.
But anyway, let's now get to the challenge.
And in this one, we're going back to Mark and John comparing
their body mass indexes.
And this time we're gonna use objects
to implement these calculations.
So here we have the formula again to calculate the BMI.
And so your tasks are, first for each of them,
so for Mark and John create an object with properties
for their full name, their mass and their height.
So their full names are actually Mark Miller and John Smith.
Then create a method called calcBMI on each object
which will then calculate the BMI based on the data
that is on the object.
So create the both method and add the exact same method
to both objects.
So write this method first in one of the objects
and then copy the exact same method
without changing it to the other object.
And this method should store the BMI value
to a property and it should also return it from the method.
So a little bit like the cogH method
that we did in the previous lecture
then lock to the console who of the two has to hire a BMI
and this string should look like this.
So it should include the full name
and actually here it's not the full name
but do include the full name that's why we have it.
So John Smith BMI is higher than Mark Miller's.
So not killers, but Mark Millers
and then also include the actual BMI value here.
And here is the test data
and I believe it's the same data as before.
So good luck with this challenge.
And again, it might be challenging
but please give it a try anyway, okay.
So I see you here in a minute
or maybe in half an hour, who knows?
It doesn't matter how long you take.
Now, okay, so let's get to work.
So number one is creating an object for each of them.
So object Mark.
So full name property, Mark Miller,
then the mass which is 78 kilograms
and then the height which is 1.69 meters.
So that's it for now.
And now I'm just gonna copy this,
as always, I'm a bit lazy
and that's just how programmers are, I guess.
So John Smith and he is 92 kilos
and 1.95 meters tall.
Okay, so that is point number one.
And now we will create the cog BMI method.
And then a function without any parameters
because we will take the data directly from the object
just like we did it with the cog H method previously.
And remember that it says here that we should store
the BMI value to a property and also return it
from the method.
So this means that we will do this .BMI
and then do the calculation here.
And remember, from the formula to BMI is calculated
by this .mass divided by this .height squared.
So exponentiation operator with two
and then also return it.
So at this .BMI
and just to see if this actually works,
let's actually call this method.
So mark.calc.BMI
and now I will quickly lock to the console,
the property that we just created.
So I could have locked this to the console directly
but I want to do it this way now.
And it doesn't really matter.
So with this, remember we created a property called BMI.
And so that is now of course accessible here.
So mark.BMI because remember that here in this method,
this now points to Mark because that is the object
that is calling the method
and therefore this .BMI will be on Mark.
So mark.BMI equals mark.mass divided by mark.height.
And so that's how we get this data into this method.
All right, so that's essentially what we learned
in the last lecture.
And so here I'm doing the same.
I'm first calculating this value
and then I'm doing mark.BMI
which was added here in this method call.
Of course without this method call,
mark.BMI would not be available
because this method here it doesn't call itself.
We need to explicitly call it.
Now, console are missing the .log.
How can that still happen after all these years?
Okay, so here we get the BMI of 27 which seems about right.
And now as I said, we will simply go ahead
and copy this method exactly the same to this John object
and it will work just the same, believe me.
So let me show that to you.
So now we call John.calcBMI.
And so now the object calling calcBMI is John.
And so now in this method here,
this will point to the John object.
And so this time John.BMI will be calculated
using John's data of mass and height.
Okay, are you still following that?
As always, you can pause the video,
analyze the code on your own
or you can always rewind to hear the explanation again.
I actually like to do that myself
when I watch other online video courses.
So I think that's a good tip maybe.
Anyway, so we just calculated the BMI of John here as well
and so now I'm logging both to the console.
And so yeah, this one also looks correct.
And so this means that simply copying this method
from one place to the other actually makes it work
and adapt to the data of each objects.
Now having the exact same code in both of these objects,
kind of violates the don't repeat yourself principle, right.
And actually there is a way in which we can avoid this.
And of course we will learn how
at a later point in the course
using something called object oriented programming.
But for now we are still learning the basics
and just want to make things work.
And of course have some fun along the way.
So this was the hardest part I believe
at least now all we have to do is to write this string here.
Let me actually copy the examples
so we have it as a reference down here.
Otherwise I'm gonna get a bit lost.
So if Mark.BMI is larger than John.BMI, then console.log.
So let's get the full name now from the object.
So mark.fullName BMI and now we also need it here
in these parenthesis.
So let's put it there, Mark.BMI is higher than
and I'll let me just copy it here.
So and again, I'm using command D
to select the next occurrence
and then let's just replace it with John.
So Mark's BMI is higher than John's BMI.
And now let's grab this again.
Else if John BMI is larger than Mark.BMI
then essentially say the opposite.
So here it's gonna be John and again command D
and here it's gonna be Mark, okay.
So 27 is higher than 24 and that 27 belongs to Mark.
And so this seems to be about right
but it's not completely right.
So of course here it should be lowercase.
So BMI and now I cannot use the command D
because then it will select this one as well.
So I'm double clicking on this one
and I hope that you're finding
these VS code tips useful as well.
So I'm clicking or actually I'm double clicking on this one,
Then I hold down the command key
which in your case might be the control key
or maybe the option key.
So I know there are different configurations for this
and it might not even work for you.
So please don't ask any question in that case
but for me, I just have to hit command
and then I can double click on multiple words
and then all of them get selected.
So now I have these four multiple cursors here again
and I can simply delete all of them
And right the one that I'm interested in.
so that's the lowercase BMI
and now we get to correct values
And also this conditional logic is correct as well.
And so it is we did actually solve the challenge
and with this we also finished this introduction
to JavaScript objects.
And once again, I really want to congratulate you
for the progress that you're making here.
Great job and really well done.
And I really mean it because I know how hard it is
to stick to a course like this
and to learn a completely new thing
that is probably a bit confusing
for you at this point still.
Just keep in mind that that is absolutely no problem at all.
And actually in the next section I believe,
I have a video about how to learn,
how to learn basically so about learning how to code.
And there's a lot of great tips that I'm sharing there.
So once you reach that, make sure to watch it carefully.
But now anyway, I will leave you to review
this lecture if you want or to move on together with me
to the next video
where we're now gonna start talking about loops.