(Instructor)This is coding challenge number two.
And in this one,
we're gonna go back to Julia in case study about dogs.
And this time, they want to convert the ages of the dogs
to human ages, and then calculate their average age.
So your job is to create
this calc average human age function.
And this function accepts an array of dogs ages.
And it does the following four tasks,
or actually just three tasks.
So first, calculate the age of all the dogs
in human years using this formula.
So if the dog is less or equal two years old,
then the human age is simply twice the dog's age.
But if the dog is older than two years,
then the human age is calculated by this formula.
All right.
Then in the next step, exclude all the dogs
that are less than 18 human years old.
And that is essentially the same as keeping the dogs
that are at least 18 years old here okay.
Then, finally,
calculate the average human age of all the adult dogs.
And from the other challenges, you should already know how
to calculate averages.
Now, in this one, you will do it
in a different way, hopefully,
because the goal of this challenge
is of course to use the Map, Filter and Reduce methods.
Finally, then run this function
for both these test data sets.
So as I said, this is a way of training the map filter
and array methods that we just studied earlier.
And so good luck with this challenge.
Take all the time you need and they see you here
once you're ready.
So were you successful in solving this one?
I sure hope so.
And in case there was some doubt, here is my solution.
So this function takes an array of dog Ages.
And then we want to convert all of these ages to human ages.
So what we want here is to create
a new array based on the original array.
And so for that the map method is perfect.
So we will have a new array called Human Ages.
And that will be the result of calling the map method
on the ages array.
Then this callback function gets access to the current age,
so the current element in the array.
And now here what we need to return
is the result of this formula.
So this one here,
and so let's write a ternary operator here.
So if the age is less or equal two,
then what we want to return here is two times the age,
right?
Because that dog age is of course, this age variable here,
because it comes from the ages array,
which itself is an array of dog Ages.
Anyway, if the age is greater than two,
then the result will be 16 plus the age times four.
And so since this has an implicit return here, remember,
it's like writing this here,
okay?
And so it's returning either this value here or actually,
either this value, so twice the age,
or four times the age plus 16.
So that's the callback function,
and this will then produce the array that we're looking for.
Let's take a look.
So human ages, and then we just need to call the function.
And so here is the age converted to human ages.
And it does indeed look correct.
So let's assume it is correct.
Then next up we want to filter for all the dogs
that are at least 18 years old.
And so that will then exclude all the dogs
that are less than 18 years old.
So that's gonna be the adult dogs and so that is human ages.
And now we use the filter method and that makes sense right?
Because it's the filter method who allows us
to set a condition like this one.
So when the age is at least, so great or equal 18,
then put it in the new array which will be the adults array.
And so let's lock that one as well.
So actually, I want both of them here.
So human ages
and the adults.
And so indeed, these two here,
the four and the two are gone.
Alright.
And now finally, we want to calculate the average.
Okay?
And so the average is basically adding all
of these values together,
and then dividing the result by five,
so the length of the array.
So that's essentially what we already did before.
So that's gonna be adults.reduce,
then the accumulator and the current age.
And we want to return the accumulator plus the age.
And the start is gonna be at zero.
So this here will return the sum,
so all of these ages here added together.
That's now all we need to do is to divide
this by the length of this array.
And then we simply want to return
this average value that we just calculated.
So let's just see if this makes sense.
Let's actually save it here in a variable,
instead of logging into directly.
So average one,
and then also
average two.
And then just log both here to the console.
And all right, here they are.
And this looks pretty reasonable to me.
And so our solution here works.
Now, I just wanted to let that we could have done
this average calculation here in a different way.
So in a bit more complex way.
So let me show you one property of an average.
So let's say we want to calculate the average
of two and three.
So we do that
by two plus three.
So we add all of this together and then divided by two,
because we have two values.
And so that is then 2.5.
However, we can also do it in a different way.
So we can divide both of them by two immediately.
So two divided by two,
plus two divided by three,
and this will also be 2.5.
So these here are essentially the exact same thing.
Okay, and so here, we can do this differently.
So let me actually duplicate this line
So that we can keep both here.
So what I mean is that we can actually simply divide
the current age here immediately by the length of the array.
Right?
Here, this is actually three divided by two.
Okay, sorry for that mistake.
And so what we did here was basically
to divide both of the values always immediately by the two.
And so that would be the same as immediately dividing
the age here by the length of the array.
So dividing this by the length of the array.
And now remember that we can get
this length immediately here from this parameter.
Well, not really the length, but the array itself.
So that's the fourth parameter.
And so now we can say array.length, like this.
And so see what happens now here.
And indeed, the values are exactly the same.
And so this way of calculating it
is just the same as this one here,
but we can do it kind of all in one line.
And I also wanted to show you a good use case,
for using this array here.
In this case, we could of course,
also just have written here, adult.length,
but sometimes when we change
these methods together especially
then this is the only way we have
of using the current array.
And we will see a use case of that later.
Now of course, I did not expect you to write it like this,
but I just wanted to show you a good use case
for the current array parameter that we have here.
Okay, and with that, we finish the challenge,
and I see you in the next video.