Jonas: Welcome to Coding Challenge number three.
And this is gonna be a quick and easy one.
So all I want you to do is to rewrite
the 'calcAverageHumanAge' function
that we wrote in the previous challenge.
Now the difference here is that this time,
you should use an arrow function
and you should use chaining.
Now to make this chaining work,
just keep in mind the second way that I used
in the last challenge to calculate the average.
Then, as always, use this test data here
to try out your function.
And so I'll see you here in a minute,
or five minutes maybe, once you're done.
Now all right, so let's grab the code here
from the previous one.
So there's no need to just rewrite everything.
So let's get rid of all the comments here,
all the console.logs.
And so let's save it here,
and then rewrite it as an arrow function.
So it still takes in the ages,
and now what it returns is simply the ages,
then dot, this map,
so calculating the human ages.
Okay, and then on the result of that,
we filter for only the adult docs.
So when the age is greater than 18.
So this is what we have for now.
Let's call this one here two.
Okay. And so for now this is the two erase that we have
one for each function call.
But now let's then also calculate the average.
And so for that all we need to do is to chain
this method of calculating the average.
That he showed you in the last lecture,
or actually in the last challenge.
So pasting that here.
And so now we get the exact same results as before,
and that's actually already it.
Now what's really important to notice here,
is that this is actually really the only way
of calculating the average now.
So basically dividing the age by the length
of the current array immediately here
in each of the iteration.
So remember that in the last challenge,
I first simply added all the values together.
And then afterwards I divided it by the length of the array.
So in this case, by the length of the adults array.
However, that would not work in this case.
Because there would be no way of knowing the length
of the adults array.
So of this result here, right?
Because we haven't stored it anyway.
And so we couldn't do like this, like adults.length, right?
This wouldn't be possible
because adults here does not exist anywhere.
And therefore we need to take that length
from this input here.
So from this fourth parameter,
which gives us access to the array,
on which the method is called.
And that's the reason why I showed you this way here,
in the last lecture.
Because yeah, again this is the only way
in which we can now calculate this average,
unless we wanted to do it in two steps.
But my instruction here
was to really do it all with chaining.
And so this is the way to go then.
All right, so make sure you understand
really why and also how this one here works.
And then let's already move on to our next method,
which is gonna be defined method.
So another really cool one.
So I can't wait to see you there.