Jonas: So we've covered a lot of ground
and learned so many more array methods
over these last few lectures.
And so now it's time for yet another coding challenge
so that you don't forget all of these methods
before we move on to learning even more of them.
And this time around Julia and Kate are studying
the activity levels of different dog breeds.
And so here are your seven tasks, plus one bonus task.
Now these are a lot,
and so I'm not gonna read them here one by one.
So you can just, after you pause the video,
read each of the tasks and then complete them one by one.
Now the test data here is actually a lot.
So it's this huge array of objects.
And so if you don't want to type
all of this here one by one,
please make sure to go to the final files
of this section
and just copy this data structure from there.
And I believe that you already did the same thing earlier
with some other data structure
in some of the other challenges.
All right, so again, here are your tasks.
And so pause the video right now
in order to complete them one by one.
I just need to tell you that some of these
are actually not that easy
and you really need to combine many of the methods
that we have learned up until now.
So for some of these,
you will really have to think quite a bit,
but that's also why these are called challenges, right?
If they were super straightforward,
then you also wouldn't learn a whole lot.
But in any case,
if some of these you were not able to complete,
that's absolutely no problem at all.
As always, just watch the solution a bit later, all right?
But please at least try and pause the video right now
and I see you back here in half an hour or so.
All right, let's get started.
So hopefully the first one was not that hard.
So let's create that variable huskyweight.
And so the task here was to get the average weight
of the huskies and then just store it in its variable.
So basically what I wanted you to do is to find the object
where the breed is husky and then you just store
or you just get the weight from there and store it here.
So we just take the breeds objects.
Well, actually we need to copy this down here,
or I will just close off the comment here.
Remove this, and I guess this one is not so important.
So let's just do breeds.find.
And then each one in there is a breed, let's say.
And then we get breed.breed.
So it could be also called here the name.
So basically the name of the breed,
but it also works like this.
And so then this one should be equal to Husky.
So this one right here.
So this husky weight right now
will be this object right here.
And so from there, we now want to take the average weight.
So lock this to the console.
And so this should give us 26.
And beautiful, there we go.
So this is number one.
Let's move on to number two
and find the name of the only breed
that likes the activities of running and fetch.
And so this one is actually a little bit more complex.
So let's do again, breeds.find,
because the instruction is pretty clear
that this one is again about finding,
and it makes sense that only one breed will be found here
because the find method will indeed only find
the very first occurrence
for which the callback is true in the array.
So just as we learned in the lecture about the find method.
But anyway, here again, we have or callback function now.
And so here, remember,
we now need something that returns a Boolean.
So here we have these activities in these arrays.
And so basically what we want is to find a breed
where the activities array
contains both running and fetch.
So let's do that actually here manually.
So this is gonna be the one.
So this is the Dalmatian or whatever this is called.
And so this activities array here
contains both running and fetch,
which is exactly what the task demands.
And this is the only one.
And so this will be the result.
But now we need to, of course, find this programmatically,
but we actually already set what we need.
So again, this array here needs to contain
both running and fetch.
So let's just write that here in our callback.
So breed.activities needs to,
and it's not called contain, but includes in JavaScript.
And so it needs to contain, so to include fetch.
And now it needs to also include running.
So we just do the same thing again.
So breed.activities.includes,
and then running.
So both of these will return a Boolean.
So true, false.
So this one here will return true for many of them.
So this one here has fetch, so it'll be true,
in this case, it'll be true here
and here and in many others.
But then here we have this and.
And so this one will also return true or false.
And so then only the one for which this is true
and this is true, will become a true this entire expression.
And so then that's the breed, that will be fine.
Let's also store it in the variable
that is set in the instruction,
which is dogBothActivities.
Let's store that in here
and log to the console
exactly this.
And indeed it is this one with this name right here.
And let's actually only get the breed itself.
So .breed,
and beautiful.
So done.
Again, maybe a little bit more difficult, this one here.
But you can do it.
And if not, again, no problem in just watching the solution
as you're doing right now.
Next in number three,
we should create an array called allActivities.
Let's write that here right away.
allActivities, which as the name says,
should contain all the activities of all the dog breeds.
So for this we can just use a map.
So breeds.map
and then breed.
And then here we want to just get the breed.activities.
Right?
Now, we're not done here yet, but it's a start.
So let's see what we have so far.
And so here we have this array of arrays.
So these are exactly all of these activities right here.
However, the instruction says that we should have one array
of all the activities, not an array of arrays.
And so here is where we can use that flat method
that we also just learned about.
And so now we indeed get this one array.
Now, as an alternative, whenever we use map and then flat,
we can just use the flatMap method instead.
So which is basically a two in one.
So let's just replace this.
We'll comment this one out.
And so again, as I just mentioned, let's use flatMap,
then we can remove this one
and we get the exact same result.
Beautiful.
That's done as well.
Onto the next one, which says, create an array,
uniqueActivities that contains only the unique activities.
So basically no repetitions of the activities
as we have right now.
So currently we have running here multiple times,
we have fetch, fetch, here fetch again.
And so of course, this doesn't make so much sense.
So let's create that new variable,
uniqueActivities.
And this one is pretty easy.
So for this, we can just make use of the property
that sets are always unique.
So all the values in a set are unique.
So we can just create a new set
and pass in the all activities that we already have,
again, logging it to the console to see our progress.
And so now we get this set
which contains only the six different activities that exist.
And so now finally, we can convert this to an array.
So let's just select this here
and then just spreading all the elements
of the set into the new array.
And so then here, this is our final result.
So mark, this one as done as well.
And so moving on, we go to number five,
and this one is one of the more complex ones,
or I would even say
really the most difficult of all of them.
So if you didn't get this one right,
that's really no problem.
But anyway, the instruction reads, many dogs like to swim,
or actually many breeds like to swim.
And now here the idea is to store all the other activities
that these breeds like to do.
So the ones that like to swim in a unique array
called swimming adjacent.
So this sounds a bit complex.
So let's go here.
So let's see all the activities that have swimming.
So this one here.
So this one, this one, and this one.
These are the three breeds that like to swim.
And so now the idea is to find the adjacent activities,
which are the activities that these dogs also like to do.
So that's fetching.
So fetch.
Here again, fetch,
and then running and agility.
So this is gonna be our result.
And so let's now try to see how we can achieve that.
So let's call this here swimmingAdjacent
as the instruction sets
and so breeds.
And then the first thing to do is probably
to find all of the breeds that like to swim.
So that makes sense, right?
So we use a filter for that.
So breed, and then we say breed.activities.includes again,
swimming.
So let's see if we're underwrite.
Trek with console.logs(swimmingAdjacent),
and
yeah, so these are exactly the three breeds
that we manually identified earlier.
Now what we need to do next is to get these activities
out of these objects.
So basically having an array
where these activities are then stored.
And so this means that we just use a map.
So again, breed.
And then here, we just want to get the breed.activities.
And so this looks a lot better,
but just like before, we don't want an array of arrays,
but we want all of these activities in one array.
So we could just chain on a flat,
but instead, let's use a flatMap.
And nice, this is starting to look really good.
Let's just now actually make them unique.
So basically doing the same thing as before,
wrapping all this in a set
and in an array.
So creating a new array,
which will be actually need to keep this selected.
So parenthesis and then new Set.
And actually that's not correct
because now we need to spread these elements
into this newly created array.
And beautiful.
Now we have these adjacent activities
that we identified earlier up here, right?
So that was exactly what we have there.
Fetching,
and fetching again, and running and agility.
Let's just remove the swimming here
because that's not really adjacent.
We want the other activities.
But that's very easy.
We just do another filter in the end.
So at this point, these are called activities.
So each element of the array is now an activity.
And so let's say activity should be different from swimming.
And just like this,
we solved this challenge or this task number five.
So this looks pretty complex here,
but if you build it step by step
like we did here, also while looking at the data structure
that we're working with,
it becomes a little bit easier at least.
So let's move on to number six now,
which is just a simple question.
Do all the breeds have an average weight
of 10 kilos or more?
Simply log to the console whether this is true or false.
Now, since we need a Boolean value here, so a true or false,
some great candidates for the method
that we need here are the every and the some methods.
There are some other methods
that also give you a Boolean like the includes method
that we have used here a few times,
but this one doesn't make so much sense in this case.
And by the way, later in the section,
there is a very handy lecture
which summarizes all of these different array methods.
In fact, I think it's 30 array methods.
And there you will find basically a small decision framework
where you can more easily find out
which of the array methods you should use in each situation.
But now back to this question right here,
the question asks whether all the breeds
have some average weight, right?
And so whenever you hear this all,
then the method to use is probably the every method.
So let's just log to the console here.
And so then breeds.
And as I just mentioned every.
And so, every only becomes true
if all of the elements in the array satisfy the condition
that we're gonna specify here in the callback.
So if the callback is true for every single element,
then the every method will also return true.
While in the some method,
only one of the elements in the array needs to be true
in order for the whole expression to become true basically.
So in order for some to become true.
But anyway,
once again, each element in this array is a breed.
And so we're asking whether the average weight
is 10 kilos or more.
So average weight, right?
Yeah, so all of these have an average weight
and we're now gonna check whether it is greater than 10
for all of them.
So we can actually see that this is going to be true, right?
Because the one that has
the less a weight is that beagle.
So let's see, greater than 10.
And indeed it is true.
Let's just change something here.
Let's make this just one kilo.
And so now it is false.
So if just one of all of these elements here
does not satisfy the condition,
then the whole thing becomes false.
So it's a little bit like the AND operator,
but applied to all of these different array elements.
While the some method is more like the OR operator.
And speaking of the some method,
I think this one is gonna be handy in the next task.
So task number seven says,
are there any breeds that are active?
And active simply means that this dog has three
or more activities.
So three or more activities.
Let's see if there are any.
And actually there are not.
So this is gonna be false.
So let's check,
breeds.some,
breed
and then breed.activities.length greater than three.
And here it is laying more like this.
And so now we get a problem here,
and that's because here it is breed and not breeds.
And so indeed it is false.
Now, actually, I think
the active means that there's three or more.
So it is not more than three,
but actually more or equal three.
So
greater or equal.
And yeah, now that is true.
And again, that's because this some method here
is a little bit like the OR operator.
So it is enough for one of the array elements
to return true here in this callback
for the whole thing to become true as well.
And indeed, there are actually two dogs in this condition.
These two have three activities each.
Great.
So as I mentioned, this is actually a long one.
So this video is already running for some time,
but we still have a bonus question to answer,
which I believe was also pretty difficult.
So let's see.
The task in this case is to find out
what is the average weight of the heaviest breed
that likes to fetch.
So let's once again go to our data structure.
And so basically we're gonna select all of the breeds
that like to fetch,
and then we just want to find the heaviest of all of them.
So the one which has the largest average weight.
So deconstructing a problem into these multiple parts
always makes it a lot easier to understand
what we need to do.
So let's first of all, find out these different breeds
that like to fetch.
Let's call them the fetch breeds.
And this is pretty easy,
this part.
So we've done this many times before.
So here the breed
and then breeds.activities.includes,
and then fetch.
Let's see what we got.
And indeed, these are the five that we identified earlier.
And so now we need to in the next step get their weights.
So once again, for that, we use the map method.
So breed, and then we just get breed.averageWeight,
and then we get all of their weights.
Let's actually call this here, breed...
Or fetchWeights,
and here as well.
That makes a bit more sense.
And now all we need to do
is to find the largest value of these.
And for that, we actually have a hint,
which is to use the Math.max method
along with the spread operator.
So let's see what that is all about.
So const, and then let's call this one heaviestFetchBreed.
And so as I just mentioned, let's try to use Math.max,
and then pass in the fetchWeights.
And here, then let's just only...
Well actually, let's keep both of them here
just so we can see that the result is correct,
but actually we get a not a number.
And so that's because this Math.max function right here
does not simply accept an array
and will give us the largest value.
That's not how it works.
The way this works is that we give it multiple values
and it'll simply give us the largest of them.
So we've done this before.
Let me just demonstrate that to you
so we can pass as many values as we want here.
And of all of these arguments that we pass it,
it'll just give us the largest ones.
So how do we do this?
Basically, how do we take the values out of the array?
Well, that's where this spread operator comes into play,
because what this operator does is basically just like this,
it takes the values here and places them.
Well, now it's gone,
but it places them right there
into this Math.max function here as arguments in this case.
And so now indeed, we get 32 as the largest of these values.
Great.
So this was a lot of work, but I hope also a lot of fun.
I really liked designing this challenge for you
and now also solving it.
So a lot of different methods we used here.
And yeah, really nice challenge here as well.
So I hope you had some fun,
and I see you in the next video
where we keep working
with some very important array methods.