Okay, and let's start
by grabbing the calc tip function from earlier.
So that should be here.
Okay.
And that's why we keep all the code
in one neat file like this.
Next up, we create the bills array
and this one has quite a number of values
but we can just copy them.
They're already almost in the correct format
so no and, but a comma.
Then we created the empty tips and totals,
const tips equals an empty array
and const totals, another empty array.
And now is where the magic starts.
So basically where we will calculate the tips
and the totals.
So that is question number three.
So use calctip to calculate tips and total values.
So essentially what we will do is to loop
over the bills array and then fill up these two arrays.
So for let i equal zero
and we want this to run while the counter is less
than the length of the bills, which is 10.
And so the length index is nine.
And so that's the last element that's gonna be taken.
And now in here, let's start
by calculating the tip and store it in a variable.
So const tip is equal
and now we're using the calc tip function.
So we call it using parenthesis.
And now we need to pass in the bill value.
And what is the bill value?
Well, it is just the bill's array at the current position.
And now you might wonder why I am able to declare
this tip variable here as a constant.
Am I not changing it in every loop iteration?
Well, actually that's not what happens.
Actually, in each iteration,
a new tip variable is gonna be created.
So we're not mutating, we're not changing the original tip
that we declare here.
In each iteration, we will simply create a new one
and that's why we can use const.
Anyway, now we have the tip calculated
and so we just need to push it into the tips array.
So tips dot push and tier the tip.
And finally we need to push into the totals.
So totals dot push D tip plus the current bill.
And we already know that that is bills, right?
Okay. And that's actually it.
Now, you could have done it in a different way, for example,
without this variable here at all.
You could have just put this code here and here
but then you would've calculated the value twice.
And that's just not necessary.
We just calculated once
and then we use it both to push the value
into the tips and into the totals.
Okay? And now as always
let's check all the three arrays out in the console.
And whoa, that's a lot of numbers.
But just by quickly looking at them
everything looks correct.
Great, that's really great.
We basically just did the same
as we did in the previous version of the challenge
but this time we did it using a loop instead of manually
creating the array by calling calc tip with bills zero,
one, and two.
So that's what we did in the previous challenge
but this one is a lot better because now no matter how many
bills we have here, the loop will take care of all of them
at the same time, basically.
Okay? So that's the basic challenge.
I hope you arrived at the same result that I did
no matter how you solved it.
And now let's go for the bonus here.
So we're writing a function called calc average.
So const calc average.
And once again, I'm using a function expression here
which is my favorite kind of function I would say.
And as we said, we will receive an array
and any array here will work.
So once again, we're building a generic function
which does not care what kind of values it's gonna receive.
It can receive bills or tips or scores or heights
or masses or whatever really.
It doesn't care what the values are.
And that's also why I gave it this generic name.
So it's just calc average and not, for example,
calc average totals.
Let's not do what we say or what I say here in 4.1,
which is to start creating a sum variable that starts
at zero.
Then as we loop over the array, we will keep adding
the current value to that variable.
So this time it needs to be a let, because this sum
will of course be updated as we loop over the array.
And now all of this is pretty standard at this point.
So equal zero, I should stay below the array
dot length.
And so this time array is simply the array
that we're receiving in this function.
So it's this parameter.
And now as I said, as we loop over the array
we will keep adding the current value to the sum.
So what I mean is sum will be equal,
the current value of sum plus the array that we received
as an input at the current position.
And that's it.
We're just missing here, the increment of the counter.
But that's easy to fix.
And this one here could be even more condensed.
So let me just copy this one
and comment out the original one because remember
that we have a more shorthand operator in JavaScript
which we can use when we want to update a value.
So instead of sum equals sum plus,
remember we can write sum plus equal array i.
So this one is exactly the same as this.
So if you prefer, you can write just this version
but this here is gonna do exactly the same thing.
Okay, and just to see if this works,
let's actually test this now
for now by logging only the sum, so console dot log sum.
And now again, to test it,
I will just call the function calc average
with simply an array of some values.
So two plus three plus six.
And we know that this will be 11, right?
And so now we can test this function here just to see
if this first part that we just wrote works.
Let's see.
And yes, we get 11.
And so this logic here is correct.
So we got this array here into the function, okay?
And so R is now two, three, six.
And so that's the array that we're then looping over.
Okay, so we end up with the sum
and now what we want to return is simply the sum
divided by the number of elements in the array
which remember is R dot length.
And that's it.
Can now get rid of this but now we need to log the result
to the console if we want to see it.
Because this function here now does not log it.
It simply returns it.
And remember that that is a big difference.
So we know that the sum of this is 11
and 11 divided by three is something like close to four.
So we could change this here to seven
then the sum would be 12 and 12 divided by three is four.
And so then we can make sure that this function works.
So it is four, so great.
This means that the function now works.
And so now we can use this function to actually
calculate the average total.
And so here we simply use the totals value
that we already created before, and that's it.
So the average total value of all
our bills plus tips is 275 and 19.
And let's actually do the same with the tips themselves
just to get an idea of the kind of tips that were paid here.
So the average tip is 42.89.
Hopefully that part here was not too confusing
but if you go slowly through the code step by step,
you will understand it.
All right. And with this,
we finished the complete JavaScript fundamentals.
So we made a ton of progress here.
So congratulations for making it this far.
Great job, really.
However, as you know,
we are really just getting started here.
There is so much more to learn.
And actually in the next section, we will quickly
configure our VS code editor a little bit better
and also set up a better development environment.
So that's gonna be super useful
for writing code a little bit faster.
And I will also teach you some developer skills
that every developer should really have.
But as always, before moving on,
make 100% sure that you understood all these fundamentals
from this section and a previous section.
So if you want, you can redo the challenges or you can
come up with your own small problems or ideas to solve.
And by doing that,
you'll reinforce all these tools even better
and ultimately become a better developer faster.
Then when you're really ready, let's together move forward.