Jonas: 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 calcBMI 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 calcH 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 the BMI is calculated
by this.mass
divided by
this.height
squared.
So exponentiation operator
with two
and then also return it.
So 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.
Alright,
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
doesn't call itself.
We need to explicitly call it.
Now console
ah, 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 object.
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 example
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.fullNameBMI.
And now we also
need it here in these parenthesis.
So let's put it there.
mark.bmi
is
higher than
and now 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 ah,
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 write
the one that I'm interested in.
So that's the lowercase bmi
and now we get the correct values
and also this
conditional logic
is correct as well.
And so with this,
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 are 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 are now
gonna start talking about loops.