Narrator: And finally, here goes the second part
of my solution.
And we left off, after creating the John object,
including this huge calcTips method, okay?
Now for Mark, this is all going to be very similar
and so I'm just going to go at
and copy this entire object...
And then do some changes to it.
So the name, of course, is not John,
but Mark and it's Mark Miller.
Then this is $77, $475, okay?
And 110 and finally, 45.
So he only went to four restaurants.
Then down here, in the calcTips,
all that we have to change is,
like these tipping rules, okay?
So let's see how Mark does it.
So, he liked to tip 20% when the bill is less than $100,
10% if it's between 100 and 300,
and 25 if it's more than 300.
Let's translate that into our code.
So here, it's 100, so less than 100 it's 20%,
or actually less than 100.
Okay?
Then if it's between 100 and 300,
he gives 10% as a tip and, if it's even greater than 300,
then he gives 25.
Probably because he's at a very fancy restaurant
and then he needs to give more tips.
(laughs)
Something like that.
Okay, so that is the Mark object and it's already done.
So let's do the calculation here for Mark as well
and then log both John and Mark to the console.
So let's check it out.
Indeed, here we have our tips and our final values, alright?
So this worked.
Already we now have the tipping completely implemented
for both Mark and John.
So let's take a look at what the next step is
and it is to create a function to calculate the average
of a given array of tips and then, of course,
calculate the average for each family using that calculator.
Then finally, we can log to the console
which of the families paid, on average, the highest tips.
Alright?
So let's create a function
and I'm going to do it here after this object.
So, function calcAverage.
It's going to receive a tips array, okay?
So now I'm going to do what I told you as a hint.
So I'm going to start with a variable called sum,
which will start at zero, okay?
So in the beginning, of course, or sum, is zero.
Now what we have to do is to loop over the tips
and then, in each iteration,
add the current element to the sum.
Let's implement that.
So before and once more we start
with a variable called I at zero,
with I less than the tips of length...
And then we increment I.
So this is pretty standard.
You're seeing, at this point,
that it's basically always the same here, right?
Now, what we do is that we say that the sum
should be equal to the current sum,
plus the tips and your current position.
Okay?
Make sense?
So let's say that we pass in on the array
of two, six, and four.
Alright?
So in the beginning our sum will be zero
and then we will enter the loop.
At this point, we have that the sum is zero,
plus the current element,
which is two and so zero plus two will be two.
So the new sum will be two.
So let's just write it down here.
We start off, of course, with zero.
After that, we have two and so the next iteration,
we have two here at the sum and then the current element
of the loop is six.
So this one here, right?
So six plus two is then eight, okay?
Eight is assigned to that sum of variables, okay?
Then in the next iteration sum will be eight
and eight plus the current element of the array,
which is four, will then finally be 12.
Okay?
That's it.
We went through the entire array and edited all
of these elements together and, in the end,
we ended up with 12, which of course is the result
of adding all of these three together.
Okay?
That's just an example just to demonstrate
how this function works.
Okay, so at the end, all we have to do is to return
the sum divided by the elements that are in the array.
So tips.length.
Okay?
For example, in the array that we had before,
it had three elements and so tips.length would be three.
The average would then be 12 divided by three,
which would be four.
Okay?
So that's how you calculate averages.
We have now our objects, we have our calcAverage function
and so now, let's do all the calculations.
We already calculated the tips here,
then we logged them through console just to make sure that
they are correct, then we can also calculate the average.
What we're going to do here is to actually calculate
the average and then store them inside
of each of the object, okay?
I'm going to say John.average is calcAverage,
using John.tips, okay?
And why John.tips?
Well, that's because this is the array in which all
of the tips are stored, right?
So that is the array that we defined here.
So this dot tips, okay?
So we patch that into the calcAverage function,
which will then return the average and we will store that
into the average property on the John object.
Now you may ask, well, why didn't we add this
calcAverage here as a method to each of the objects?
Well, we didn't do that because that would
be duplicate code, so we have to have the exact same code
on both of these objects.
We would basically repeat ourselves
with the exact same code and that's not good, okay?
So it's best to just leave it outside here,
as this function, then calculate it outside
and then only then, basically, add it to each object.
We do it with John and then
we're also going to do it with Mark.
Alright, so let's actually do this console log here
after that so that we also can see
the newly added averages, alright?
So let's try that now and, wait, where is it?
Here!
Here we go.
Here's the average of the three
or actually the four paid tips.
We have these values here and the average of them is 18.
In case of Mark, these are the four tips
and the average of that is 38,
which is significantly higher,
simply because of this huge tip here with $118.
Alright.
These are the calculations for the averages
and now all we have to do is to really do the console.log.
So if John.average is larger than Mark.average
then we can say, console.log of course, so John.FullName,
now the escape, in order to escape the quotation here.
John's family pays higher tips with an average of,
and now the average tip.
(clicking)
Okay.
If it's the other way around, let's just copy it...
It's Mark here, so Mark.average is greater
than John.average, well then it's,
of course, the other way around.
Mark and Mark here as well.
Alright.
That should be our final result and in it,
Mark Miller's family pays higher tips
with an average of $38.
Okay?
Just to test this, let's decrease this one here
from 110 or, actually, this one here.
Let's increase it from 475 to like five bucks.
So the average should then decrease and so, yeah,
so now John Smith family pays the higher tips,
with an average of 18.
Alright.
That is the solution to this quite bigger
and maybe a bit more difficult and confusing challenge,
but I hope that you liked it and that you
were able to at least do part of it.
I understand if you didn't do it all, okay?
So that's, again, no problem.
You shouldn't feel bad about it.
The most important part is that you try it here
and that you put in the time to try to solve the challenge.
That's really all that matters at this point.
I'm serious here, okay?
That's really what matters most.
Anyway, you're now done with the first
JavaScript introduction.
Congratulations on that for making it this far
in the course.
That's already a huge achievement.
Now, some JavaScript courses stop at this point
and consider that you know enough
to write JavaScript on your own.
But of course, that's very far from the truth.
Now don't get me wrong,
you have already learned a lot by now, okay?
But we are really just getting started,
with a lot of great content coming up
over all of the next sections.
There's actually even one more video coming up
in this section, which is just about JavaScript versions,
so make sure to not to miss that one
because it's also important to know that kind of stuff.
It's not about coding, but it's still important, okay?
So I hope that you take a look at that right now.