Jonas: So, did you manage to successfully complete
the challenge? Well here is the way that I did it.
So, I'm going to start by creating the 'john' object
with the object literal syntax,
let's give it some more space down here.
Alright,
so then the full name property
will be: 'John Smith'.
Then the 'mass' is 92kg
and the 'height' is
1m and 95cm, so one ninety five
and then we need to calculate
BMI method, so this is the trickier part, right, but its
still not really difficult, okay. So we use a 'function'
without any argument and that's because we are gonna read
the 'mass' and the 'height' both from the current 'object'.
So, how are we going to do this?
Well, the formula is
'this' dot 'mass' okay,
divided by
'this' dot 'height' times 'this'
dot 'height' so the 'height' squared, okay, so remember the
'this' here means the current 'object' so it will read the
'mass' from the current 'john' 'object'
and so the 'mass' here will be 92
and then the same here for 'this' dot 'height' and of
course 'this' here as well. So, here it will be 1.95 times
1.95 and where do we want to store this result?
Well, it would be in this dot BMI.
So, remember, we can not only
read properties from the object using 'this' we can also set
them. And so here set 'this' dot BMI shall be a result of
this formula, okay. And that's actually it, so we calculate
the BMI then remember here from the text that we should
also return it. So, save the BMI to the object and also
return it from the method. And so,
return 'this' dot BMI,
okay, so that's 'john' and now I'm just going to go ahead
and copy it for 'mark'
because the method is actually going
to be exactly the same, obviously, okay so its going to have
no difference. The formula and everything works just the
exact same way and so we have the exact same method on
the 'mark' object. Here its Mark Miller
then the mass is just 78
and the height is
1.69, okay, and as I mentioned
the rest stays the same, so we are now ready to calculate
the BMI for both of them, so 'john' dot 'calc' BMI, remember
that's how we call methods and then the same for 'mark'.
Okay.
And so, yeah lets take a look at these two objects actually.
So, 'console' dot 'log'
both 'john' and 'mark',
So, we want to get both of these 'objects' here
into our 'console'. So, here they are.
And we see that both of them now have their BMI
so John has a 24
and Mark has a 27, so great. So, you see that its
actually very nice to have a method directly attached to the
'object' because this function is really related to this
'object', to this person in this case.
So, it makes sense that
we can have the method really together with the data instead
of having an external function which could do these
calculations as well, okay. So that's why we make use of
this pattern, lets say, this 'best practice' to have the
methods inside of the 'object'. Now your probably thinking
that having the exact same method on these two 'objects'
kinda violates the drive principal that I showed you before
so the don't 'repeat yourself principal'
and that's actually correct
so this is not the perfect way of doing it. But, for now,
with the knowledge that we have at this point, this is the
best that we can do. Later, in a more advanced section of
the course, I am then going to teach you how we can have the
exact same method on multiple objects without having to
repeat the code. But for now this is more than okay like
this, okay. So, finally we have to 'log' to the 'console',
so what do we want to say? Who has the highest BMI together
with the full name and the respective. Don't forget they
might be like a draw, so the same BMI.
We have done this many times before,
so, 'john' dot BMI lets now read it here
from the object if it's greater than 'mark' dot BMI,
well,
then lets 'log' to the 'console' that 'john' dot
'first name', or
actually it's 'full name'
has a higher BMI of
'add' and 'john'
dot BMI to read it again.
Alright now in 'else if' here
because there can be the case where they both have the
same and so we have to account for that as well. So,
now its 'mark' dot BMI is larger than 'john' dot BMI
and then lets just grab this piece here of code,
paste it here.
This should be 'mark', and this one as well.
And then just the 'else' in case it is the same.
'They have the same BMI'.
Okay so, according to our data, Mark Miller has the
highest BMI and in the 'it' that's exactly what we log to
the 'console'. So 'mark' has 27 as you can see here
and 'john' has 24.
So lets now increase 'john's mass here
to, lets say, 110
and so now in the 'it'
he has the larger BMI of both.
So, it is already almost perfect but we can do
even better, okay, so lets get first rid of this and what
we can do is actually calculate the BMIs right here.
So we calculate the BMIs using the 'calc' BMI function,
of course, or the method,
I should say. And then we use
that data to take these decisions here but keep in mind
that these methods they actually also return the BMI,
so, we could replace 'john' dot BMI here by the method call
so lets actually do that. So I'm going to actually cut it
here and paste it down here and same for 'mark'.
Alright, so does that make sense to you?
So again, we know that this 'calc' BMI method here
will 'return' the BMI themselves,
we have it here, so 'return' 'this' dot BMI. So, down
here we will calculate the BMI and then immediately 'return'
it and it will then be put here in this logical comparison.
The same of course for 'mark', in this case,
'this' here will then be replaced with 27.3,
which is what we see here and
this 'john' dot 'calc' BMI will be 28.9,
right, so this 'value' here.
Then the comparison can happen and everything can just
work the same. So we don't need these additional lines
where we first calculate the BMI and only then we read it.
So we can basically calculate it and read at the same time.
Here then we can read it already because at this point it is
already calculated and so of course we are not going to
call BMI method again. We only want to call it once and
then from there on we can just directly read the 'value'
from the 'object'
so we are not going to put 'calc' BMI here or here
because at this point, again, it is already calculated.
Does that make sense to you? Of course, again, you
wouldn't have to do it this way so its not really
important but that's actually the reason why I asked
you to also 'return' the 'values' here from the methods so
that we can then use this little trick where we can
calculate and compare the 'values' here all in one take.
Just to confirm that it works the same of course,
here we go, with the correct solution, great.
So, we are almost at the end of the section,
there is just one more thing to learn,
which is the 'Loops' and 'Iteration' which is exactly what
we are going to do in the next video.
I hope you enjoyed this one, see you next time.