-: (Instructor) Welcome to the final coding challenge
of this section.
And this one can really be seen
as a final test of this section.
So we're going to practice most
of what we've been doing here and so let's get started.
And in this challenge,
we are back to Julia and Kate studying dogs.
And this time they are studying
whether dogs are eating too much or too little.
So eating too much means that the dogs current food portion
is larger than the recommended portion.
And eating too little is the opposite.
And then there's also eating an okay amount.
And so that's when the dog's current food portion
is within the range 10% above,
and 10% below the recommended portion.
And we will check that out in detail in a second.
And so here are your tasks.
And actually, each of them can be seen
as a separate challenge.
So there are eight of them.
So this is going to be a long one.
And some of them are actually a little bit tricky.
So a bit hard to solve and that is one more time on purpose.
So that you can actually challenge yourself,
and try to do a little bit more
than just what we learned here in these videos.
And as always, if you cannot solve one of them on your own,
then just go ahead and watch the solution,
and then go back to the next point.
Now anyway, let's get started here.
And the first task is to loop over the array
that contains the dog objects.
So that is this object here.
And this is the data that we're going to work with.
So it's this array called dogs.
And in each position of the array,
we have one object per dog.
So your task is to loop over that array.
And then for each dog to calculate
the recommended food portion,
and then to add it to the object as a new property.
So do not create a new array here,
just loop over the array that we already have,
and add the recommended food portion as a property.
And so I hope you know which array method
you need to use for that.
And to calculate that recommended food portion,
simply take the weight of the dog
and elevate it to 0.75 times 28.
Then next up, find Sarah's dog and log to the console,
whether it is eating too much or too little.
Now in this one, it's important to keep in mind,
or important to notice that these owners
are actually inside an array.
And so this makes this task a little bit more tricky.
And so again, that is on purpose.
Next up, create an array containing
all the owners of dogs who eat too much.
So call this one owners eat too much
and then an array with the opposite.
So with owners of dogs who eat too little.
Then log a string to the console
for each of the arrays that we created previously like this.
So Matilda, Alice and Bob's dogs eat too much.
So that's based on this array.
And then based on this array, a string like this.
Then afterwards, log to the console,
whether there is any dog eating an okay amount of food.
And so let's take a look
at what an okay amount of food actually means.
So here we have to send two
and so being within the range 10% above
and 10% below means that the current food portion,
which is stored here in this property
should be greater than the recommended food portion
that you calculated before times 0.9.
So that's 90% and at the same time,
it should be less than 110% basically,
of the recommended amount.
So these two conditions here at the same time
mean that the dog is eating an okay amount of food.
So basically, it's what we see here.
So the current portion should be between 90
and 110% of the recommended.
Then number seven, create an array containing
all the dogs that are eating an okay amount of food
and I want you to reuse the condition
that we created before here are a number of six.
Finally, create a shallow copy of the dog's array
and then sort it by the recommended food portion
in an ascending order.
Now just keep in mind
that the portions are inside the arrays objects.
Okay, so to sort this array,
it's a little bit trickier than what we did before.
Because yeah, the value is now here inside of an object,
and not just in the array itself.
But the way that we write
the callback function is pretty similar.
So we will still have the ANP parameters,
but these are no objects.
And so to compare the recommended food portion,
you will have to get that property
out of each of these objects.
So yeah, that's it.
It's probably going to take you some time.
But what matters most
is that you practice and that you don't give up.
But anyway, let's take now like half an hour,
or even one hour to solve this one
and then I see you back here.
So let's get started with the solution.
So the first one is to calculate
the recommended food for each of the dog.
Now, as I said, you should not create a new array,
but simply loop over the already existing array.
So this dogs array, actually copy it here.
So copy.
So looping over this array and then to each object,
we want to add a new property.
And so the for each method is the way to go here.
Then each element is a dog.
And what we want to do is to add a property.
So dog.recommended food is equal
to dog.weight elevated 0.75 times 28.
And now let's take a look at the dogs.
And indeed, now they have the recommended food.
Now let's just get rid of this decimal part here.
So let's say here, math.trunc.
So we use this one before we simply truncate.
So we cut the decimal parts.
And so that's number one and now number two,
let's check it out again.
So we need to find Sarah's dog and then log to the console,
whether it's eating too much or too little.
So basically, we need to find the object here.
So in this case, that's going to be this one.
So we need to find this object,
and then compare the recommended food
we just calculated with the current food.
So the trickiest part here is going to be to find the owner.
But I believe we can do it.
So let's call this one dogSarah, then it's dogs.
And we use the find method,
because we want to find one element
of the array based on a condition.
So in each iteration, we have one dog.
And now we want to take dog.owners.
And now this itself is also an array.
So it is this here.
And what we want now is for this array to include Sarah.
And so we can do this, includes Sarah and that's it.
This is how we get our true or false value.
So remember that in the callback of the find method,
we need to return a true or a false value.
And then the find method will return
the first element for which the condition is true.
And in this case, we get the true or false value
by calling the includes method on the owners array.
And so when the array of owners includes Sarah,
then that condition is true.
And then we are going to get this object.
So let's just make sure here and let's see.
And indeed, it is the correct object.
We need some more space here.
This we don't need any more.
And now we just need to log to the console
whether the dog is eating too much or not.
And we already know that it's eating too much
because the current food is 275 and the recommended is 191.
So let's log Sarah's dog is eating.
And here I'm going to use my old friend,
the ternary operator, Sarah.current food.
If it's greater, then dogSarah.recommended food.
Well then here we want to have much,
because in this situation,
the dog is clearly eating too much.
Now of course this needs to be here inside the curly braces.
And here now it's correct.
So we're asking basically this question.
So if the current food is more than the recommended,
then the dog is eating too much and else too little.
And so indeed, Sarah's dog is eating too much.
So that's the conclusion we had already reached
by looking here at the numbers.
Okay, moving on.
Let's take a second here to look at the exercise again.
So create an array containing
all owners of dogs who eat too much.
So let's copy this name.
And then also one where they eat too little.
So how are we going to do that?
Well, we know that we want a new array
based on the existing dog's array.
And we want to create this new array based on a condition.
So once again, we want to compare the current food
to the recommended food.
And therefore the correct method to use here is the filter.
So in each iteration, we get a dog.
And we're going to return whether this dog's current food
is greater than the dog recommended food.
Because here we are looking for the dogs that eat too much.
And so that means that the current food
is greater than the recommended.
So this will give us the dogs.
But let's start by taking a look at this variable for now,
even though we want the owners.
So this one doesn't show it to us.
But yes, so this dog here
is eating more than the recommended.
Now, we now want to get the owner or actually the owners.
So again, we want to create a new array
based on an existing array.
And this time, this new array should
have the same length as before.
We simply want to take something out of the original array
and put it into the new array.
And so that means we're going to use map.
So once again, we have a dog
and we want to return the dog.owners.
And now we have an array of arrays,
where one is Matilda, and the other one is Sarah and John.
And so finally, the next step,
to transform all of this into just one array,
is to call flat.
And so indeed, we end up with this array
with just one level.
And with all the owners of all the dogs that eat too much.
So great and maybe you can see here
that we can actually do even better,
because whenever we have a map combined with a flat,
we can instead use flat map.
So let's transform this year into flat map.
And then we don't even need this part
and the result is the same indeed.
And based on this one, we can easily create the owners
where the dogs eat too little.
So all we need to do is to invert this sign.
Let's get rid of this one here.
And so these are the other three owners basically.
Next up, our task is to create some strings.
So let me just copy the strings.
And so that's based on the arrays that we just created.
So these are the two strings.
And actually they will not be the same.
So this is just an example.
So here we see Alice, Bob and Michael's dogs are together,
and here they are not.
So the string will be different, but that doesn't matter.
So what matters is that here we have dogs eat too much.
And so now all we do, is to take the owners
that we already have eat too much.
And essentially join them together
using the separator string of and, like this.
And so now we get this array
here Matilda, Sarah and John dog.
So let's just add this eat too much.
And now the other way around eat too little
and here too little as well.
Beautiful.
So we're making good progress here,
let's move on to number five.
So log whether there is any dog eating exactly
the amount of food that is recommended.
So whenever you see the word any,
you can think of the sum method.
So remember that the sum method
will return true if at least one of the elements
in the array satisfies a certain condition.
So log to the console, dogs.sum.
And so one more time,
we are comparing the current food with the recommended food.
And this time, we want to know if there is at least one dog
eating exactly the same amount as the recommended portion.
And so we use the equal operator here.
So current and recommended food.
And well, there is none.
Let's take a look at our dogs here just to confirm that.
So these are different, these are different,
these are different and also probably yeah,
this is also different.
And therefore, if all of them are false,
then the sum method also returns false.
Number six, log to the console,
whether there is any dog eating an okay amount of food.
And so now this formula here comes into play.
Let's copy it just so we see it down here.
And so once again,
the goal is to simply return true or false,
whether there is at least one dog
eating an okay amount of food.
So we need the sum method again.
And now this a little bit more complex condition.
So dog.current food portion
needs to be greater than the recommended times 0.9.
So that's 90% and then at the same time,
so this, (laughs) now let me just copy it here.
So at the same time,
the dog should also be eating less than 110%
of the recommended amount.
And so now we get true.
So there is at least one dog but we don't know how many.
And so that's the task basically of the next point,
which is to filter out all the dogs
that are actually eating this okay amount of food.
And for that we should also export
this condition into a separate function.
So this is really just a function.
So we can take it and store it into its own variable.
And let's call it check eating okay.
And then simply the function.
And so here, we can then use that function.
And yeah, we still get the same result.
And now as I was just saying,
the goal here is to create an array
containing only the dogs that are eating
an okay amount of food.
So we want a new array based on an existing array,
but only with some of the elements.
And so it is time again for using the filter method.
So docs dot filter and now this is very easy,
because we already have this condition
callback function here in this variable.
And well, we only have one dog in this condition as you see.
So that's Michael's dog,
where the recommended food is indeed pretty close
to the current food.
And so we consider that this is an okay amount of food
to eat for the dog.
Okay, almost ready here.
So I told you this is a long one.
But I hope you're having fun with this one.
This kind of seems like a real world challenge.
So like a real world task, doesn't it?
So it took quite some time and designing this challenge.
And even that was also a lot of fun.
But anyway, let's now create a copy of the dogs array,
and then sorted by the recommended food portion.
So let me actually copy this
so we don't forget what we have to do here.
So we start by creating a copy.
So dogs.copy, no dot of course,
just the name is dogs copy.
And so let's use dogs.slice for that.
And actually, we can call the sort method
then immediately on that.
So here, we are going to need a callback function as always,
which is the sorting function.
And this always has a and b as the parameters.
So we could call them something else of course here,
but I like to always call them a and b.
And actually, we don't even need this block here.
So remember that before,
when we wanted to sort an array in an ascending order,
we simply would do a minus b like this.
But this was when we had an array, simply like this.
So just with numbers,
but now we have these numbers here in objects.
And so a and b, are now the objects.
However, the values that we want to subtract
are in these objects and we can simply
get them out of there.
So we want to sort by the recommended food portion.
And so let's simply do this.
And that's actually it.
Let's call this your dog's sorted.
And then let's log that to the console here.
Dog's sorted.
Let's see and yeah, that worked.
Oh, actually we cannot see it yet.
So yeah, here is the recommended food.
So we start with the lowest one, then 191.
And you see that indeed, it is in an ascending order.
So 284 and 376.
So that worked.
And so we finished this coding challenge.
This was a lot of work.
And as I just said, also a lot of fun.
And I hope you felt the same.
And also that you felt the same for this entire section.
So this project was a great project in my opinion,
it was a lot of fun to build it together with you,
and to put it together and design it in the first place.
So I hope you're proud of what you achieved here.
And one more time I want to congratulate you
for making it this far and still sticking
with this quite long course.
Anyway with that, I see you in the next section,
where we will actually continue building
on this project by adding a couple of new things to it.