Jonas: Welcome back to the first coding challenge
of the arrays section.
And in this one, Julia and Kate are doing a study on dogs.
So each of them asked five dog owners about their dog's age
and then they stored that data into an array.
And that's one array for each of them.
All right?
And in this study, they are interested in knowing
whether each of the dogs is an adult or a puppy.
And the rule is that a dog is an adult
if it is at least three years old,
and it's a puppy if it's less than three years old.
And by the way, the test data here,
so the arrays for it is down here.
So test data one and two.
All right?
So, your task is to create a function called check dogs,
which accepts these two arrays
and does the following things.
So first, Julia found out that the owners of the first
and the last two dogs do actually have cats and not dogs.
So here, what I want you to do
is to create a shallow copy of Julia's array
and then remove the cat ages from the copied array.
And you should copy it because it's a bad practice
to mutate the parameters or the arguments of a function.
Then second, create an array
with both Julia's and Kate's data.
So for Julia, of course, the corrected data.
And then for each of the dogs,
simply log to the console whether it is an adult
with a string like this
or a puppy and then with a string like this.
And that's, of course, according to the rules
in the beginning.
And then, just run this function for both test data sets.
And that's it.
So all you need to do is to use the tools
from all the lectures in this section so far
and then I think you will be fine solving this on your own.
So this is really all about using the tools
that we already learned before
and there's not a lot of new thinking to do here.
So I hope you manage to do this one.
And I see you back here in a second.
All right.
So hopefully, you did this just fine without any problems
but I will anyway write my solution here
and explain what I am doing.
So, we accept the dogs from Julia
and the dogs from Kate.
And we will call this with the test data.
And so, Julia's data is this
and there's not a need to store them in separate variables.
I will just call the function like this.
All right?
And now we should create a shallow copy of dogs Julia,
so that we can then remove some elements from there.
So dogsJuliaCorrected.slice
and so I'm using here the method
and not the spread operator.
And now, I will simply remove some elements from there.
So dogsJuliaCorrected.splice
and so, it tells us that the first
and the last two dogs are not valid basically.
So, the first is element number zero
and then we want to remove one.
So that's the first part.
And then dogsJuliaCorrected.splice
and then the last two, which is minus two.
Okay?
So, let's take a look here simply at
dogsJuliaCorrected just to see
if this is correct actually.
So we need the console now.
And so this was or beginning data and indeed,
the first and the last two are done here.
All right?
Now, of course, you could have also done this
in a different way,
which is to simply copy the correct data
from the original using the slice method.
So something like this: dogsJulia.slice,
so starting from the second one
and then all the way to the third one.
Okay, and so this would give us the exact same result
but in this case, I just wanted to use the splice method.
And, in fact, I wanted you to use the splice method as well
and that's why I gave you that task like that.
Anyway, we don't need this here anymore.
Let's now simply create a new variable with both these dogs.
So simply calling it dogs.
And again, we could use the spread operator here
which I actually prefer
but since we are in the arrays section here,
let's do everything with array methods.
So that's the concat method, remember?
And then we want to join
the corrected Julia dogs with dogs Kate.
So always a good idea to check.
And yeah, that's correct.
All right?
And so, all we have to do now is
basically create this string.
Let me just copy this string.
And so, looping over this array is now an easy task.
Hopefully.
So dogs.forEach
then the callback function
and one more time,
we need actually the current element and also the index.
And that's because here is the number.
So dog number one and it's going to go all the way to seven.
And so, let's check out our rule.
So, a dog is an adult if he is at least three years old.
So if dog is greater equal three,
then log a string like this.
So here is the index plus one.
Here goes the dog itself
and then else
is still a puppy.
And here we need just this.
And, I think, with this we are good.
Just remove this.
And yes, dog number one is five years old,
so it's still an adult.
Number two is two years old, so still a puppy.
This is again a puppy with one year old
and so, yeah, that's the correct solution.
Now let's just do it for this test data here.
Gonna duplicate this one.
Let's just comment out the first one
and okay, here we only have one puppy
which is the one before the last.
And so this one is correct as well.
And so, challenge completed.