WEBVTT

1
00:00:01.310 --> 00:00:04.780
<v Jonas>Welcome back to the first coding challenge</v>

2
00:00:04.780 --> 00:00:06.573
of the arrays section.

3
00:00:08.410 --> 00:00:13.380
And in this one, Julia and Kate are doing a study on dogs.

4
00:00:13.380 --> 00:00:17.960
So each of them asked five dog owners about their dog's age

5
00:00:17.960 --> 00:00:21.830
and then they stored that data into an array.

6
00:00:21.830 --> 00:00:25.070
And that's one array for each of them.

7
00:00:25.070 --> 00:00:26.420
All right?

8
00:00:26.420 --> 00:00:29.370
And in this study, they are interested in knowing

9
00:00:29.370 --> 00:00:33.620
whether each of the dogs is an adult or a puppy.

10
00:00:33.620 --> 00:00:36.610
And the rule is that a dog is an adult

11
00:00:36.610 --> 00:00:39.430
if it is at least three years old,

12
00:00:39.430 --> 00:00:43.123
and it's a puppy if it's less than three years old.

13
00:00:44.060 --> 00:00:46.280
And by the way, the test data here,

14
00:00:46.280 --> 00:00:50.000
so the arrays for it is down here.

15
00:00:50.000 --> 00:00:52.700
So test data one and two.

16
00:00:52.700 --> 00:00:54.100
All right?

17
00:00:54.100 --> 00:00:58.570
So, your task is to create a function called check dogs,

18
00:00:58.570 --> 00:01:00.900
which accepts these two arrays

19
00:01:00.900 --> 00:01:02.573
and does the following things.

20
00:01:03.930 --> 00:01:08.530
So first, Julia found out that the owners of the first

21
00:01:08.530 --> 00:01:13.530
and the last two dogs do actually have cats and not dogs.

22
00:01:13.650 --> 00:01:15.430
So here, what I want you to do

23
00:01:15.430 --> 00:01:18.910
is to create a shallow copy of Julia's array

24
00:01:18.910 --> 00:01:23.040
and then remove the cat ages from the copied array.

25
00:01:23.040 --> 00:01:26.040
And you should copy it because it's a bad practice

26
00:01:26.040 --> 00:01:29.653
to mutate the parameters or the arguments of a function.

27
00:01:30.750 --> 00:01:32.580
Then second, create an array

28
00:01:32.580 --> 00:01:35.950
with both Julia's and Kate's data.

29
00:01:35.950 --> 00:01:39.280
So for Julia, of course, the corrected data.

30
00:01:39.280 --> 00:01:40.940
And then for each of the dogs,

31
00:01:40.940 --> 00:01:44.700
simply log to the console whether it is an adult

32
00:01:44.700 --> 00:01:46.780
with a string like this

33
00:01:46.780 --> 00:01:50.160
or a puppy and then with a string like this.

34
00:01:50.160 --> 00:01:52.008
And that's, of course, according to the rules

35
00:01:52.008 --> 00:01:53.830
in the beginning.

36
00:01:53.830 --> 00:01:57.920
And then, just run this function for both test data sets.

37
00:01:57.920 --> 00:01:59.430
And that's it.

38
00:01:59.430 --> 00:02:01.980
So all you need to do is to use the tools

39
00:02:01.980 --> 00:02:04.990
from all the lectures in this section so far

40
00:02:04.990 --> 00:02:08.830
and then I think you will be fine solving this on your own.

41
00:02:08.830 --> 00:02:11.260
So this is really all about using the tools

42
00:02:11.260 --> 00:02:13.270
that we already learned before

43
00:02:13.270 --> 00:02:16.743
and there's not a lot of new thinking to do here.

44
00:02:17.600 --> 00:02:19.650
So I hope you manage to do this one.

45
00:02:19.650 --> 00:02:21.703
And I see you back here in a second.

46
00:02:25.520 --> 00:02:26.353
All right.

47
00:02:28.860 --> 00:02:32.880
So hopefully, you did this just fine without any problems

48
00:02:34.880 --> 00:02:39.120
but I will anyway write my solution here

49
00:02:39.120 --> 00:02:41.760
and explain what I am doing.

50
00:02:41.760 --> 00:02:45.510
So, we accept the dogs from Julia

51
00:02:45.510 --> 00:02:48.143
and the dogs from Kate.

52
00:02:51.550 --> 00:02:55.240
And we will call this with the test data.

53
00:02:55.240 --> 00:02:57.570
And so, Julia's data is this

54
00:02:57.570 --> 00:03:01.530
and there's not a need to store them in separate variables.

55
00:03:01.530 --> 00:03:03.603
I will just call the function like this.

56
00:03:07.770 --> 00:03:08.930
All right?

57
00:03:08.930 --> 00:03:12.963
And now we should create a shallow copy of dogs Julia,

58
00:03:14.720 --> 00:03:17.623
so that we can then remove some elements from there.

59
00:03:18.840 --> 00:03:21.323
So dogsJuliaCorrected.slice

60
00:03:23.816 --> 00:03:27.150
So dogsJuliaCorrected.slice

61
00:03:28.090 --> 00:03:30.660
and so I'm using here the method

62
00:03:30.660 --> 00:03:33.290
and not the spread operator.

63
00:03:33.290 --> 00:03:36.880
And now, I will simply remove some elements from there.

64
00:03:36.880 --> 00:03:41.440
So dogsJuliaCorrected.splice

65
00:03:43.030 --> 00:03:47.110
and so, it tells us that the first

66
00:03:47.110 --> 00:03:51.773
and the last two dogs are not valid basically.

67
00:03:52.700 --> 00:03:57.140
So, the first is element number zero

68
00:03:57.140 --> 00:03:59.343
and then we want to remove one.

69
00:04:00.760 --> 00:04:02.093
So that's the first part.

70
00:04:02.980 --> 00:04:07.900
And then dogsJuliaCorrected.splice

71
00:04:08.900 --> 00:04:11.333
and then the last two, which is minus two.

72
00:04:12.200 --> 00:04:13.033
Okay?

73
00:04:13.970 --> 00:04:17.268
So, let's take a look here simply at

74
00:04:17.268 --> 00:04:22.040
dogsJuliaCorrected just to see

75
00:04:22.040 --> 00:04:24.193
if this is correct actually.

76
00:04:25.470 --> 00:04:27.670
So we need the console now.

77
00:04:27.670 --> 00:04:32.020
And so this was or beginning data and indeed,

78
00:04:32.020 --> 00:04:35.930
the first and the last two are done here.

79
00:04:35.930 --> 00:04:37.260
All right?

80
00:04:37.260 --> 00:04:39.760
Now, of course, you could have also done this

81
00:04:39.760 --> 00:04:41.250
in a different way,

82
00:04:41.250 --> 00:04:43.830
which is to simply copy the correct data

83
00:04:43.830 --> 00:04:46.653
from the original using the slice method.

84
00:04:48.180 --> 00:04:51.317
So something like this: dogsJulia.slice,

85
00:04:52.838 --> 00:04:55.960
So something like this: dogsJulia.slice,

86
00:04:55.960 --> 00:04:58.450
so starting from the second one

87
00:04:58.450 --> 00:05:00.623
and then all the way to the third one.

88
00:05:02.710 --> 00:05:06.270
Okay, and so this would give us the exact same result

89
00:05:06.270 --> 00:05:10.630
but in this case, I just wanted to use the splice method.

90
00:05:10.630 --> 00:05:14.850
And, in fact, I wanted you to use the splice method as well

91
00:05:14.850 --> 00:05:17.763
and that's why I gave you that task like that.

92
00:05:19.140 --> 00:05:22.270
Anyway, we don't need this here anymore.

93
00:05:22.270 --> 00:05:26.663
Let's now simply create a new variable with both these dogs.

94
00:05:27.600 --> 00:05:29.510
So simply calling it dogs.

95
00:05:29.510 --> 00:05:32.000
And again, we could use the spread operator here

96
00:05:32.000 --> 00:05:34.180
which I actually prefer

97
00:05:34.180 --> 00:05:37.010
but since we are in the arrays section here,

98
00:05:37.010 --> 00:05:39.283
let's do everything with array methods.

99
00:05:41.110 --> 00:05:44.080
So that's the concat method, remember?

100
00:05:44.080 --> 00:05:45.440
And then we want to join

101
00:05:46.350 --> 00:05:50.023
the corrected Julia dogs with dogs Kate.

102
00:05:54.380 --> 00:05:56.473
So always a good idea to check.

103
00:05:57.340 --> 00:05:59.933
And yeah, that's correct.

104
00:06:02.170 --> 00:06:03.120
All right?

105
00:06:03.120 --> 00:06:05.810
And so, all we have to do now is

106
00:06:06.890 --> 00:06:09.270
basically create this string.

107
00:06:09.270 --> 00:06:10.993
Let me just copy this string.

108
00:06:12.500 --> 00:06:16.920
And so, looping over this array is now an easy task.

109
00:06:16.920 --> 00:06:18.370
Hopefully.

110
00:06:18.370 --> 00:06:20.237
So dogs.forEach

111
00:06:22.420 --> 00:06:24.250
then the callback function

112
00:06:24.250 --> 00:06:25.410
and one more time,

113
00:06:25.410 --> 00:06:29.993
we need actually the current element and also the index.

114
00:06:31.240 --> 00:06:34.760
And that's because here is the number.

115
00:06:34.760 --> 00:06:38.763
So dog number one and it's going to go all the way to seven.

116
00:06:40.070 --> 00:06:42.483
And so, let's check out our rule.

117
00:06:43.340 --> 00:06:47.893
So, a dog is an adult if he is at least three years old.

118
00:06:51.290 --> 00:06:56.203
So if dog is greater equal three,

119
00:06:57.720 --> 00:07:02.483
then log a string like this.

120
00:07:08.890 --> 00:07:11.313
So here is the index plus one.

121
00:07:15.505 --> 00:07:19.960
Here goes the dog itself

122
00:07:19.960 --> 00:07:21.040
and then else

123
00:07:24.260 --> 00:07:25.403
is still a puppy.

124
00:07:30.470 --> 00:07:32.573
And here we need just this.

125
00:07:34.110 --> 00:07:36.673
And, I think, with this we are good.

126
00:07:37.720 --> 00:07:38.793
Just remove this.

127
00:07:40.510 --> 00:07:45.030
And yes, dog number one is five years old,

128
00:07:45.030 --> 00:07:47.050
so it's still an adult.

129
00:07:47.050 --> 00:07:51.200
Number two is two years old, so still a puppy.

130
00:07:51.200 --> 00:07:53.480
This is again a puppy with one year old

131
00:07:55.140 --> 00:07:59.450
and so, yeah, that's the correct solution.

132
00:07:59.450 --> 00:08:02.423
Now let's just do it for this test data here.

133
00:08:05.295 --> 00:08:07.653
Gonna duplicate this one.

134
00:08:14.380 --> 00:08:16.180
Let's just comment out the first one

135
00:08:18.270 --> 00:08:21.280
and okay, here we only have one puppy

136
00:08:21.280 --> 00:08:24.110
which is the one before the last.

137
00:08:24.110 --> 00:08:26.640
And so this one is correct as well.

138
00:08:26.640 --> 00:08:28.933
And so, challenge completed.

