Jonas: Welcome back to the last and most exciting
coding challenge of this section.
So, remember the tip calculator
that we created in another challenge.
Let's now create a more advanced version
using everything that we learned up until this point.
So this time, John and his family
went to five different restaurants.
And the bills were 124, 48, 268, 180, and 42 dollars.
And again, John likes to tip 20 percent
of the bill, when it is less than 50 dollars,
50 percent if it's between 50 and 200,
and 10 percent if the bill is more than 200 dollars.
Alright?
So, implement a tip calculator
using objects and loops this time.
So here are the steps for doing that,
in order to help you.
So, first create an object with
an array for the bill values.
Then, add a method to calculate the tip,
and this method should of course include a loop,
to iterate over all the paid bills,
and to do the tip calculations.
Then, as an output, you should again create two arrays.
One containing all the paid tips,
and two, containing all the final paid amount.
So again, the bill plus the tip. Alright?
And a hint that I have to give you here
is that you can start with two empty arrays
as properties, and then fill them up in the loop.
Okay?
So an empty array is just like this here.
And you can start with two empty arrays,
one for just number one, so all the tips,
and then two, for all the final paid amounts.
So start with these two as empty arrays
and then in a loop, you can fill them up.
Alright?
So that is already a lot of work,
but after that, I even have like a step two for you.
So, to even take it to the next level.
And that is, that Mark's family also went on a holiday.
And they are going to four different restaurants.
And the bill that they paid were
77, 375, 110, and 45 dollars.
And Mark has a slightly different
rules when it comes to tipping.
So he likes to tip 20 percent,
if the bill is less than 100 dollars,
10 percent, when the bill is between 100 and 300 dollars,
and 25 percent if the bill is more than 300 dollars.
Okay?
So again, that doesn't make much sense maybe,
but that's just again, to make it a bit more exciting,
and to make these two tipping functions
different for each of the persons.
Okay?
So, I want you to implement the same
functionality as before, but this time of course,
using Mark's tipping rules.
Then create a function, and I don't mean a method.
So, like an outside function to calculate
the average of a given array of tips.
And here I have to give you another hint,
because up until this point,
we never did anything like this.
Okay and so I give you a hint here to calculate the average.
And that is that you should loop over the array,
and in each iteration, store the current sum
in a variable which starts from zero.
Then after you have the sum of the array,
then you'll simply divide it by
the number of elements in it.
Okay? So that's the formula for calculating averages.
So if that's a bit too difficult for you,
then you can just stop maybe after point five.
Okay?
But I would like you to try out,
to calculate the average, alright?
So that's here on number seven,
so you use the function that you
created in number six to actually
calculate the average for both families.
And then you'll log to the console
which family paid the highest tips on average.
Alright?
So, again, this one is a bit tricky maybe,
so you can, no problem, take an hour or even more
to try to accomplish this, alright?
I think it's really important for your
learning process to go through
something like this, a bit more difficult even,
so that you really have to try,
instead of it being super easy.
Because then you don't really learn anything, okay?
So take an hour or something like that to
really go through this and really try
to solve these problems.
Alright? You don't need to be fast.
Then if there are some parts that
you cannot figure out, again, no problem,
you can just check out my solution in the next video.