WEBVTT

1
00:00:01.080 --> 00:00:03.450
<v Jonas>So we've covered a lot of ground</v>

2
00:00:03.450 --> 00:00:06.180
and learned so many more array methods

3
00:00:06.180 --> 00:00:08.460
over these last few lectures.

4
00:00:08.460 --> 00:00:12.060
And so now it's time for yet another coding challenge

5
00:00:12.060 --> 00:00:15.120
so that you don't forget all of these methods

6
00:00:15.120 --> 00:00:18.423
before we move on to learning even more of them.

7
00:00:19.691 --> 00:00:22.950
And this time around Julia and Kate are studying

8
00:00:22.950 --> 00:00:26.400
the activity levels of different dog breeds.

9
00:00:26.400 --> 00:00:31.400
And so here are your seven tasks, plus one bonus task.

10
00:00:31.440 --> 00:00:33.480
Now these are a lot,

11
00:00:33.480 --> 00:00:35.790
and so I'm not gonna read them here one by one.

12
00:00:35.790 --> 00:00:39.000
So you can just, after you pause the video,

13
00:00:39.000 --> 00:00:43.140
read each of the tasks and then complete them one by one.

14
00:00:43.140 --> 00:00:45.930
Now the test data here is actually a lot.

15
00:00:45.930 --> 00:00:49.170
So it's this huge array of objects.

16
00:00:49.170 --> 00:00:51.360
And so if you don't want to type

17
00:00:51.360 --> 00:00:53.820
all of this here one by one,

18
00:00:53.820 --> 00:00:57.180
please make sure to go to the final files

19
00:00:57.180 --> 00:00:58.980
of this section

20
00:00:58.980 --> 00:01:01.860
and just copy this data structure from there.

21
00:01:01.860 --> 00:01:05.580
And I believe that you already did the same thing earlier

22
00:01:05.580 --> 00:01:07.560
with some other data structure

23
00:01:07.560 --> 00:01:10.800
in some of the other challenges.

24
00:01:10.800 --> 00:01:14.190
All right, so again, here are your tasks.

25
00:01:14.190 --> 00:01:16.500
And so pause the video right now

26
00:01:16.500 --> 00:01:18.840
in order to complete them one by one.

27
00:01:18.840 --> 00:01:21.420
I just need to tell you that some of these

28
00:01:21.420 --> 00:01:23.760
are actually not that easy

29
00:01:23.760 --> 00:01:26.490
and you really need to combine many of the methods

30
00:01:26.490 --> 00:01:28.860
that we have learned up until now.

31
00:01:28.860 --> 00:01:30.090
So for some of these,

32
00:01:30.090 --> 00:01:32.760
you will really have to think quite a bit,

33
00:01:32.760 --> 00:01:36.750
but that's also why these are called challenges, right?

34
00:01:36.750 --> 00:01:39.000
If they were super straightforward,

35
00:01:39.000 --> 00:01:41.880
then you also wouldn't learn a whole lot.

36
00:01:41.880 --> 00:01:43.290
But in any case,

37
00:01:43.290 --> 00:01:46.020
if some of these you were not able to complete,

38
00:01:46.020 --> 00:01:48.240
that's absolutely no problem at all.

39
00:01:48.240 --> 00:01:53.010
As always, just watch the solution a bit later, all right?

40
00:01:53.010 --> 00:01:56.250
But please at least try and pause the video right now

41
00:01:56.250 --> 00:01:59.703
and I see you back here in half an hour or so.

42
00:02:02.730 --> 00:02:05.610
All right, let's get started.

43
00:02:05.610 --> 00:02:08.493
So hopefully the first one was not that hard.

44
00:02:09.330 --> 00:02:11.943
So let's create that variable huskyweight.

45
00:02:15.810 --> 00:02:19.230
And so the task here was to get the average weight

46
00:02:19.230 --> 00:02:24.230
of the huskies and then just store it in its variable.

47
00:02:24.480 --> 00:02:28.260
So basically what I wanted you to do is to find the object

48
00:02:28.260 --> 00:02:32.700
where the breed is husky and then you just store

49
00:02:32.700 --> 00:02:36.450
or you just get the weight from there and store it here.

50
00:02:36.450 --> 00:02:40.830
So we just take the breeds objects.

51
00:02:40.830 --> 00:02:43.173
Well, actually we need to copy this down here,

52
00:02:44.640 --> 00:02:49.233
or I will just close off the comment here.

53
00:02:50.700 --> 00:02:54.153
Remove this, and I guess this one is not so important.

54
00:02:55.110 --> 00:02:59.700
So let's just do breeds.find.

55
00:02:59.700 --> 00:03:03.690
And then each one in there is a breed, let's say.

56
00:03:03.690 --> 00:03:08.070
And then we get breed.breed.

57
00:03:08.070 --> 00:03:10.380
So it could be also called here the name.

58
00:03:10.380 --> 00:03:12.630
So basically the name of the breed,

59
00:03:12.630 --> 00:03:14.730
but it also works like this.

60
00:03:14.730 --> 00:03:19.730
And so then this one should be equal to Husky.

61
00:03:20.220 --> 00:03:22.500
So this one right here.

62
00:03:22.500 --> 00:03:25.230
So this husky weight right now

63
00:03:25.230 --> 00:03:27.630
will be this object right here.

64
00:03:27.630 --> 00:03:32.193
And so from there, we now want to take the average weight.

65
00:03:33.840 --> 00:03:35.613
So lock this to the console.

66
00:03:37.890 --> 00:03:40.440
And so this should give us 26.

67
00:03:40.440 --> 00:03:43.350
And beautiful, there we go.

68
00:03:43.350 --> 00:03:44.643
So this is number one.

69
00:03:48.240 --> 00:03:50.520
Let's move on to number two

70
00:03:50.520 --> 00:03:53.190
and find the name of the only breed

71
00:03:53.190 --> 00:03:57.300
that likes the activities of running and fetch.

72
00:03:57.300 --> 00:04:01.290
And so this one is actually a little bit more complex.

73
00:04:01.290 --> 00:04:05.610
So let's do again, breeds.find,

74
00:04:05.610 --> 00:04:08.190
because the instruction is pretty clear

75
00:04:08.190 --> 00:04:10.590
that this one is again about finding,

76
00:04:10.590 --> 00:04:14.730
and it makes sense that only one breed will be found here

77
00:04:14.730 --> 00:04:17.850
because the find method will indeed only find

78
00:04:17.850 --> 00:04:19.290
the very first occurrence

79
00:04:19.290 --> 00:04:22.770
for which the callback is true in the array.

80
00:04:22.770 --> 00:04:27.330
So just as we learned in the lecture about the find method.

81
00:04:27.330 --> 00:04:32.330
But anyway, here again, we have or callback function now.

82
00:04:33.780 --> 00:04:35.010
And so here, remember,

83
00:04:35.010 --> 00:04:38.580
we now need something that returns a Boolean.

84
00:04:38.580 --> 00:04:41.850
So here we have these activities in these arrays.

85
00:04:41.850 --> 00:04:45.090
And so basically what we want is to find a breed

86
00:04:45.090 --> 00:04:47.430
where the activities array

87
00:04:47.430 --> 00:04:50.970
contains both running and fetch.

88
00:04:50.970 --> 00:04:53.343
So let's do that actually here manually.

89
00:04:54.840 --> 00:04:57.090
So this is gonna be the one.

90
00:04:57.090 --> 00:05:01.563
So this is the Dalmatian or whatever this is called.

91
00:05:03.030 --> 00:05:05.880
And so this activities array here

92
00:05:05.880 --> 00:05:08.640
contains both running and fetch,

93
00:05:08.640 --> 00:05:12.000
which is exactly what the task demands.

94
00:05:12.000 --> 00:05:13.380
And this is the only one.

95
00:05:13.380 --> 00:05:14.880
And so this will be the result.

96
00:05:14.880 --> 00:05:18.570
But now we need to, of course, find this programmatically,

97
00:05:18.570 --> 00:05:21.420
but we actually already set what we need.

98
00:05:21.420 --> 00:05:24.210
So again, this array here needs to contain

99
00:05:24.210 --> 00:05:28.290
both running and fetch.

100
00:05:28.290 --> 00:05:30.590
So let's just write that here in our callback.

101
00:05:31.590 --> 00:05:35.850
So breed.activities needs to,

102
00:05:35.850 --> 00:05:39.963
and it's not called contain, but includes in JavaScript.

103
00:05:41.580 --> 00:05:45.870
And so it needs to contain, so to include fetch.

104
00:05:45.870 --> 00:05:49.780
And now it needs to also include running.

105
00:05:52.560 --> 00:05:54.750
So we just do the same thing again.

106
00:05:54.750 --> 00:05:58.503
So breed.activities.includes,

107
00:06:01.560 --> 00:06:03.363
and then running.

108
00:06:04.260 --> 00:06:07.410
So both of these will return a Boolean.

109
00:06:07.410 --> 00:06:08.910
So true, false.

110
00:06:08.910 --> 00:06:11.793
So this one here will return true for many of them.

111
00:06:12.630 --> 00:06:14.940
So this one here has fetch, so it'll be true,

112
00:06:14.940 --> 00:06:17.220
in this case, it'll be true here

113
00:06:17.220 --> 00:06:20.250
and here and in many others.

114
00:06:20.250 --> 00:06:21.870
But then here we have this and.

115
00:06:21.870 --> 00:06:24.687
And so this one will also return true or false.

116
00:06:24.687 --> 00:06:27.870
And so then only the one for which this is true

117
00:06:27.870 --> 00:06:32.250
and this is true, will become a true this entire expression.

118
00:06:32.250 --> 00:06:35.340
And so then that's the breed, that will be fine.

119
00:06:35.340 --> 00:06:37.920
Let's also store it in the variable

120
00:06:37.920 --> 00:06:39.840
that is set in the instruction,

121
00:06:39.840 --> 00:06:41.973
which is dogBothActivities.

122
00:06:47.940 --> 00:06:50.610
Let's store that in here

123
00:06:50.610 --> 00:06:52.030
and log to the console

124
00:06:55.230 --> 00:06:56.700
exactly this.

125
00:06:56.700 --> 00:07:00.390
And indeed it is this one with this name right here.

126
00:07:00.390 --> 00:07:03.660
And let's actually only get the breed itself.

127
00:07:03.660 --> 00:07:06.810
So .breed,

128
00:07:06.810 --> 00:07:08.253
and beautiful.

129
00:07:10.200 --> 00:07:12.150
So done.

130
00:07:12.150 --> 00:07:15.780
Again, maybe a little bit more difficult, this one here.

131
00:07:15.780 --> 00:07:17.460
But you can do it.

132
00:07:17.460 --> 00:07:20.940
And if not, again, no problem in just watching the solution

133
00:07:20.940 --> 00:07:22.560
as you're doing right now.

134
00:07:22.560 --> 00:07:23.850
Next in number three,

135
00:07:23.850 --> 00:07:26.760
we should create an array called allActivities.

136
00:07:26.760 --> 00:07:29.160
Let's write that here right away.

137
00:07:29.160 --> 00:07:32.400
allActivities, which as the name says,

138
00:07:32.400 --> 00:07:37.350
should contain all the activities of all the dog breeds.

139
00:07:37.350 --> 00:07:39.963
So for this we can just use a map.

140
00:07:41.460 --> 00:07:44.640
So breeds.map

141
00:07:44.640 --> 00:07:46.773
and then breed.

142
00:07:48.510 --> 00:07:52.250
And then here we want to just get the breed.activities.

143
00:07:54.540 --> 00:07:55.373
Right?

144
00:07:56.490 --> 00:07:59.310
Now, we're not done here yet, but it's a start.

145
00:07:59.310 --> 00:08:01.323
So let's see what we have so far.

146
00:08:03.210 --> 00:08:06.660
And so here we have this array of arrays.

147
00:08:06.660 --> 00:08:11.660
So these are exactly all of these activities right here.

148
00:08:13.020 --> 00:08:17.220
However, the instruction says that we should have one array

149
00:08:17.220 --> 00:08:20.550
of all the activities, not an array of arrays.

150
00:08:20.550 --> 00:08:24.300
And so here is where we can use that flat method

151
00:08:24.300 --> 00:08:25.983
that we also just learned about.

152
00:08:27.060 --> 00:08:31.050
And so now we indeed get this one array.

153
00:08:31.050 --> 00:08:35.760
Now, as an alternative, whenever we use map and then flat,

154
00:08:35.760 --> 00:08:39.390
we can just use the flatMap method instead.

155
00:08:39.390 --> 00:08:42.150
So which is basically a two in one.

156
00:08:42.150 --> 00:08:44.133
So let's just replace this.

157
00:08:45.450 --> 00:08:47.760
We'll comment this one out.

158
00:08:47.760 --> 00:08:52.110
And so again, as I just mentioned, let's use flatMap,

159
00:08:52.110 --> 00:08:54.510
then we can remove this one

160
00:08:54.510 --> 00:08:56.733
and we get the exact same result.

161
00:08:57.600 --> 00:08:58.620
Beautiful.

162
00:08:58.620 --> 00:09:00.210
That's done as well.

163
00:09:00.210 --> 00:09:02.910
Onto the next one, which says, create an array,

164
00:09:02.910 --> 00:09:07.910
uniqueActivities that contains only the unique activities.

165
00:09:08.160 --> 00:09:11.730
So basically no repetitions of the activities

166
00:09:11.730 --> 00:09:13.200
as we have right now.

167
00:09:13.200 --> 00:09:16.110
So currently we have running here multiple times,

168
00:09:16.110 --> 00:09:19.170
we have fetch, fetch, here fetch again.

169
00:09:19.170 --> 00:09:22.380
And so of course, this doesn't make so much sense.

170
00:09:22.380 --> 00:09:24.993
So let's create that new variable,

171
00:09:26.284 --> 00:09:28.410
uniqueActivities.

172
00:09:28.410 --> 00:09:30.030
And this one is pretty easy.

173
00:09:30.030 --> 00:09:33.540
So for this, we can just make use of the property

174
00:09:33.540 --> 00:09:36.180
that sets are always unique.

175
00:09:36.180 --> 00:09:39.000
So all the values in a set are unique.

176
00:09:39.000 --> 00:09:43.500
So we can just create a new set

177
00:09:43.500 --> 00:09:47.133
and pass in the all activities that we already have,

178
00:09:49.080 --> 00:09:52.713
again, logging it to the console to see our progress.

179
00:09:54.030 --> 00:09:56.700
And so now we get this set

180
00:09:56.700 --> 00:10:00.990
which contains only the six different activities that exist.

181
00:10:00.990 --> 00:10:04.860
And so now finally, we can convert this to an array.

182
00:10:04.860 --> 00:10:07.510
So let's just select this here

183
00:10:08.640 --> 00:10:10.980
and then just spreading all the elements

184
00:10:10.980 --> 00:10:12.900
of the set into the new array.

185
00:10:12.900 --> 00:10:16.263
And so then here, this is our final result.

186
00:10:17.520 --> 00:10:20.700
So mark, this one as done as well.

187
00:10:20.700 --> 00:10:24.030
And so moving on, we go to number five,

188
00:10:24.030 --> 00:10:27.690
and this one is one of the more complex ones,

189
00:10:27.690 --> 00:10:28.740
or I would even say

190
00:10:28.740 --> 00:10:31.440
really the most difficult of all of them.

191
00:10:31.440 --> 00:10:33.000
So if you didn't get this one right,

192
00:10:33.000 --> 00:10:35.190
that's really no problem.

193
00:10:35.190 --> 00:10:39.600
But anyway, the instruction reads, many dogs like to swim,

194
00:10:39.600 --> 00:10:42.210
or actually many breeds like to swim.

195
00:10:42.210 --> 00:10:46.260
And now here the idea is to store all the other activities

196
00:10:46.260 --> 00:10:49.080
that these breeds like to do.

197
00:10:49.080 --> 00:10:52.110
So the ones that like to swim in a unique array

198
00:10:52.110 --> 00:10:54.063
called swimming adjacent.

199
00:10:55.050 --> 00:10:56.400
So this sounds a bit complex.

200
00:10:56.400 --> 00:10:58.260
So let's go here.

201
00:10:58.260 --> 00:11:01.830
So let's see all the activities that have swimming.

202
00:11:01.830 --> 00:11:03.273
So this one here.

203
00:11:04.410 --> 00:11:06.570
So this one, this one, and this one.

204
00:11:06.570 --> 00:11:09.570
These are the three breeds that like to swim.

205
00:11:09.570 --> 00:11:13.290
And so now the idea is to find the adjacent activities,

206
00:11:13.290 --> 00:11:17.040
which are the activities that these dogs also like to do.

207
00:11:17.040 --> 00:11:18.300
So that's fetching.

208
00:11:18.300 --> 00:11:19.770
So fetch.

209
00:11:19.770 --> 00:11:21.000
Here again, fetch,

210
00:11:21.000 --> 00:11:23.520
and then running and agility.

211
00:11:23.520 --> 00:11:25.830
So this is gonna be our result.

212
00:11:25.830 --> 00:11:29.253
And so let's now try to see how we can achieve that.

213
00:11:30.720 --> 00:11:33.840
So let's call this here swimmingAdjacent

214
00:11:33.840 --> 00:11:35.740
as the instruction sets

215
00:11:36.990 --> 00:11:39.585
and so breeds.

216
00:11:39.585 --> 00:11:42.120
And then the first thing to do is probably

217
00:11:42.120 --> 00:11:45.900
to find all of the breeds that like to swim.

218
00:11:45.900 --> 00:11:47.313
So that makes sense, right?

219
00:11:48.180 --> 00:11:50.523
So we use a filter for that.

220
00:11:51.990 --> 00:11:56.990
So breed, and then we say breed.activities.includes again,

221
00:12:02.640 --> 00:12:03.990
swimming.

222
00:12:03.990 --> 00:12:06.270
So let's see if we're underwrite.

223
00:12:06.270 --> 00:12:09.447
Trek with console.logs(swimmingAdjacent),

224
00:12:13.950 --> 00:12:14.783
and

225
00:12:17.010 --> 00:12:20.130
yeah, so these are exactly the three breeds

226
00:12:20.130 --> 00:12:23.250
that we manually identified earlier.

227
00:12:23.250 --> 00:12:26.280
Now what we need to do next is to get these activities

228
00:12:26.280 --> 00:12:29.040
out of these objects.

229
00:12:29.040 --> 00:12:30.780
So basically having an array

230
00:12:30.780 --> 00:12:34.410
where these activities are then stored.

231
00:12:34.410 --> 00:12:38.313
And so this means that we just use a map.

232
00:12:39.180 --> 00:12:42.630
So again, breed.

233
00:12:42.630 --> 00:12:46.160
And then here, we just want to get the breed.activities.

234
00:12:49.680 --> 00:12:51.990
And so this looks a lot better,

235
00:12:51.990 --> 00:12:55.740
but just like before, we don't want an array of arrays,

236
00:12:55.740 --> 00:12:58.920
but we want all of these activities in one array.

237
00:12:58.920 --> 00:13:01.770
So we could just chain on a flat,

238
00:13:01.770 --> 00:13:03.940
but instead, let's use a flatMap.

239
00:13:06.930 --> 00:13:10.830
And nice, this is starting to look really good.

240
00:13:10.830 --> 00:13:14.433
Let's just now actually make them unique.

241
00:13:15.270 --> 00:13:18.180
So basically doing the same thing as before,

242
00:13:18.180 --> 00:13:19.720
wrapping all this in a set

243
00:13:20.880 --> 00:13:23.280
and in an array.

244
00:13:23.280 --> 00:13:25.023
So creating a new array,

245
00:13:26.040 --> 00:13:30.750
which will be actually need to keep this selected.

246
00:13:30.750 --> 00:13:35.343
So parenthesis and then new Set.

247
00:13:36.720 --> 00:13:38.070
And actually that's not correct

248
00:13:38.070 --> 00:13:40.320
because now we need to spread these elements

249
00:13:40.320 --> 00:13:42.390
into this newly created array.

250
00:13:42.390 --> 00:13:43.770
And beautiful.

251
00:13:43.770 --> 00:13:47.280
Now we have these adjacent activities

252
00:13:47.280 --> 00:13:52.280
that we identified earlier up here, right?

253
00:13:52.890 --> 00:13:54.930
So that was exactly what we have there.

254
00:13:54.930 --> 00:13:55.923
Fetching,

255
00:13:56.970 --> 00:14:00.540
and fetching again, and running and agility.

256
00:14:00.540 --> 00:14:02.010
Let's just remove the swimming here

257
00:14:02.010 --> 00:14:04.380
because that's not really adjacent.

258
00:14:04.380 --> 00:14:07.380
We want the other activities.

259
00:14:07.380 --> 00:14:08.550
But that's very easy.

260
00:14:08.550 --> 00:14:11.883
We just do another filter in the end.

261
00:14:13.350 --> 00:14:16.920
So at this point, these are called activities.

262
00:14:16.920 --> 00:14:19.323
So each element of the array is now an activity.

263
00:14:21.360 --> 00:14:26.250
And so let's say activity should be different from swimming.

264
00:14:30.150 --> 00:14:31.350
And just like this,

265
00:14:31.350 --> 00:14:35.373
we solved this challenge or this task number five.

266
00:14:36.600 --> 00:14:38.550
So this looks pretty complex here,

267
00:14:38.550 --> 00:14:40.710
but if you build it step by step

268
00:14:40.710 --> 00:14:44.550
like we did here, also while looking at the data structure

269
00:14:44.550 --> 00:14:46.020
that we're working with,

270
00:14:46.020 --> 00:14:48.243
it becomes a little bit easier at least.

271
00:14:50.100 --> 00:14:52.470
So let's move on to number six now,

272
00:14:52.470 --> 00:14:54.540
which is just a simple question.

273
00:14:54.540 --> 00:14:57.030
Do all the breeds have an average weight

274
00:14:57.030 --> 00:14:59.250
of 10 kilos or more?

275
00:14:59.250 --> 00:15:03.120
Simply log to the console whether this is true or false.

276
00:15:03.120 --> 00:15:07.110
Now, since we need a Boolean value here, so a true or false,

277
00:15:07.110 --> 00:15:09.840
some great candidates for the method

278
00:15:09.840 --> 00:15:14.490
that we need here are the every and the some methods.

279
00:15:14.490 --> 00:15:15.570
There are some other methods

280
00:15:15.570 --> 00:15:19.050
that also give you a Boolean like the includes method

281
00:15:19.050 --> 00:15:21.360
that we have used here a few times,

282
00:15:21.360 --> 00:15:24.930
but this one doesn't make so much sense in this case.

283
00:15:24.930 --> 00:15:27.000
And by the way, later in the section,

284
00:15:27.000 --> 00:15:29.010
there is a very handy lecture

285
00:15:29.010 --> 00:15:32.190
which summarizes all of these different array methods.

286
00:15:32.190 --> 00:15:35.490
In fact, I think it's 30 array methods.

287
00:15:35.490 --> 00:15:39.360
And there you will find basically a small decision framework

288
00:15:39.360 --> 00:15:41.610
where you can more easily find out

289
00:15:41.610 --> 00:15:45.930
which of the array methods you should use in each situation.

290
00:15:45.930 --> 00:15:49.260
But now back to this question right here,

291
00:15:49.260 --> 00:15:52.320
the question asks whether all the breeds

292
00:15:52.320 --> 00:15:55.140
have some average weight, right?

293
00:15:55.140 --> 00:15:57.870
And so whenever you hear this all,

294
00:15:57.870 --> 00:16:02.870
then the method to use is probably the every method.

295
00:16:03.240 --> 00:16:06.750
So let's just log to the console here.

296
00:16:06.750 --> 00:16:09.840
And so then breeds.

297
00:16:09.840 --> 00:16:12.060
And as I just mentioned every.

298
00:16:12.060 --> 00:16:14.850
And so, every only becomes true

299
00:16:14.850 --> 00:16:18.660
if all of the elements in the array satisfy the condition

300
00:16:18.660 --> 00:16:21.030
that we're gonna specify here in the callback.

301
00:16:21.030 --> 00:16:25.140
So if the callback is true for every single element,

302
00:16:25.140 --> 00:16:29.040
then the every method will also return true.

303
00:16:29.040 --> 00:16:30.630
While in the some method,

304
00:16:30.630 --> 00:16:34.920
only one of the elements in the array needs to be true

305
00:16:34.920 --> 00:16:38.880
in order for the whole expression to become true basically.

306
00:16:38.880 --> 00:16:41.790
So in order for some to become true.

307
00:16:41.790 --> 00:16:42.663
But anyway,

308
00:16:44.310 --> 00:16:47.793
once again, each element in this array is a breed.

309
00:16:48.630 --> 00:16:53.630
And so we're asking whether the average weight

310
00:16:54.390 --> 00:16:57.060
is 10 kilos or more.

311
00:16:57.060 --> 00:16:59.523
So average weight, right?

312
00:17:00.990 --> 00:17:03.150
Yeah, so all of these have an average weight

313
00:17:03.150 --> 00:17:06.600
and we're now gonna check whether it is greater than 10

314
00:17:06.600 --> 00:17:07.623
for all of them.

315
00:17:08.610 --> 00:17:12.750
So we can actually see that this is going to be true, right?

316
00:17:12.750 --> 00:17:14.680
Because the one that has

317
00:17:17.340 --> 00:17:19.953
the less a weight is that beagle.

318
00:17:20.940 --> 00:17:23.913
So let's see, greater than 10.

319
00:17:24.900 --> 00:17:27.780
And indeed it is true.

320
00:17:27.780 --> 00:17:29.793
Let's just change something here.

321
00:17:31.380 --> 00:17:34.410
Let's make this just one kilo.

322
00:17:34.410 --> 00:17:35.940
And so now it is false.

323
00:17:35.940 --> 00:17:40.530
So if just one of all of these elements here

324
00:17:40.530 --> 00:17:42.510
does not satisfy the condition,

325
00:17:42.510 --> 00:17:44.580
then the whole thing becomes false.

326
00:17:44.580 --> 00:17:47.820
So it's a little bit like the AND operator,

327
00:17:47.820 --> 00:17:51.363
but applied to all of these different array elements.

328
00:17:54.180 --> 00:17:57.963
While the some method is more like the OR operator.

329
00:17:59.040 --> 00:18:01.350
And speaking of the some method,

330
00:18:01.350 --> 00:18:05.670
I think this one is gonna be handy in the next task.

331
00:18:05.670 --> 00:18:07.650
So task number seven says,

332
00:18:07.650 --> 00:18:10.860
are there any breeds that are active?

333
00:18:10.860 --> 00:18:14.220
And active simply means that this dog has three

334
00:18:14.220 --> 00:18:15.543
or more activities.

335
00:18:20.340 --> 00:18:23.580
So three or more activities.

336
00:18:23.580 --> 00:18:24.993
Let's see if there are any.

337
00:18:26.340 --> 00:18:28.170
And actually there are not.

338
00:18:28.170 --> 00:18:29.943
So this is gonna be false.

339
00:18:30.840 --> 00:18:32.400
So let's check,

340
00:18:32.400 --> 00:18:35.283
breeds.some,

341
00:18:36.240 --> 00:18:37.380
breed

342
00:18:37.380 --> 00:18:42.380
and then breed.activities.length greater than three.

343
00:18:48.450 --> 00:18:52.503
And here it is laying more like this.

344
00:18:53.610 --> 00:18:56.070
And so now we get a problem here,

345
00:18:56.070 --> 00:18:59.850
and that's because here it is breed and not breeds.

346
00:18:59.850 --> 00:19:02.340
And so indeed it is false.

347
00:19:02.340 --> 00:19:03.460
Now, actually, I think

348
00:19:05.100 --> 00:19:08.433
the active means that there's three or more.

349
00:19:09.540 --> 00:19:11.550
So it is not more than three,

350
00:19:11.550 --> 00:19:14.553
but actually more or equal three.

351
00:19:15.870 --> 00:19:16.990
So

352
00:19:18.390 --> 00:19:20.940
greater or equal.

353
00:19:20.940 --> 00:19:22.830
And yeah, now that is true.

354
00:19:22.830 --> 00:19:25.260
And again, that's because this some method here

355
00:19:25.260 --> 00:19:27.570
is a little bit like the OR operator.

356
00:19:27.570 --> 00:19:30.720
So it is enough for one of the array elements

357
00:19:30.720 --> 00:19:33.300
to return true here in this callback

358
00:19:33.300 --> 00:19:35.853
for the whole thing to become true as well.

359
00:19:37.080 --> 00:19:40.560
And indeed, there are actually two dogs in this condition.

360
00:19:40.560 --> 00:19:42.933
These two have three activities each.

361
00:19:43.920 --> 00:19:44.820
Great.

362
00:19:44.820 --> 00:19:48.540
So as I mentioned, this is actually a long one.

363
00:19:48.540 --> 00:19:51.360
So this video is already running for some time,

364
00:19:51.360 --> 00:19:53.910
but we still have a bonus question to answer,

365
00:19:53.910 --> 00:19:57.090
which I believe was also pretty difficult.

366
00:19:57.090 --> 00:19:58.410
So let's see.

367
00:19:58.410 --> 00:20:00.720
The task in this case is to find out

368
00:20:00.720 --> 00:20:04.500
what is the average weight of the heaviest breed

369
00:20:04.500 --> 00:20:06.450
that likes to fetch.

370
00:20:06.450 --> 00:20:10.020
So let's once again go to our data structure.

371
00:20:10.020 --> 00:20:13.170
And so basically we're gonna select all of the breeds

372
00:20:13.170 --> 00:20:15.300
that like to fetch,

373
00:20:15.300 --> 00:20:19.170
and then we just want to find the heaviest of all of them.

374
00:20:19.170 --> 00:20:23.313
So the one which has the largest average weight.

375
00:20:26.220 --> 00:20:30.180
So deconstructing a problem into these multiple parts

376
00:20:30.180 --> 00:20:34.110
always makes it a lot easier to understand

377
00:20:34.110 --> 00:20:35.850
what we need to do.

378
00:20:35.850 --> 00:20:39.690
So let's first of all, find out these different breeds

379
00:20:39.690 --> 00:20:41.250
that like to fetch.

380
00:20:41.250 --> 00:20:44.520
Let's call them the fetch breeds.

381
00:20:44.520 --> 00:20:48.690
And this is pretty easy,

382
00:20:48.690 --> 00:20:49.650
this part.

383
00:20:49.650 --> 00:20:51.663
So we've done this many times before.

384
00:20:53.040 --> 00:20:54.540
So here the breed

385
00:20:54.540 --> 00:20:59.433
and then breeds.activities.includes,

386
00:21:00.630 --> 00:21:01.950
and then fetch.

387
00:21:01.950 --> 00:21:03.633
Let's see what we got.

388
00:21:08.760 --> 00:21:13.230
And indeed, these are the five that we identified earlier.

389
00:21:13.230 --> 00:21:17.703
And so now we need to in the next step get their weights.

390
00:21:19.260 --> 00:21:23.700
So once again, for that, we use the map method.

391
00:21:23.700 --> 00:21:27.990
So breed, and then we just get breed.averageWeight,

392
00:21:30.150 --> 00:21:32.000
and then we get all of their weights.

393
00:21:33.750 --> 00:21:36.420
Let's actually call this here, breed...

394
00:21:36.420 --> 00:21:37.413
Or fetchWeights,

395
00:21:40.500 --> 00:21:42.300
and here as well.

396
00:21:42.300 --> 00:21:43.950
That makes a bit more sense.

397
00:21:43.950 --> 00:21:45.420
And now all we need to do

398
00:21:45.420 --> 00:21:48.780
is to find the largest value of these.

399
00:21:48.780 --> 00:21:51.330
And for that, we actually have a hint,

400
00:21:51.330 --> 00:21:54.420
which is to use the Math.max method

401
00:21:54.420 --> 00:21:57.360
along with the spread operator.

402
00:21:57.360 --> 00:21:59.253
So let's see what that is all about.

403
00:22:00.090 --> 00:22:02.573
So const, and then let's call this one heaviestFetchBreed.

404
00:22:10.020 --> 00:22:14.647
And so as I just mentioned, let's try to use Math.max,

405
00:22:16.110 --> 00:22:20.010
and then pass in the fetchWeights.

406
00:22:20.010 --> 00:22:23.960
And here, then let's just only...

407
00:22:24.990 --> 00:22:29.250
Well actually, let's keep both of them here

408
00:22:29.250 --> 00:22:32.373
just so we can see that the result is correct,

409
00:22:33.600 --> 00:22:36.660
but actually we get a not a number.

410
00:22:36.660 --> 00:22:40.950
And so that's because this Math.max function right here

411
00:22:40.950 --> 00:22:43.170
does not simply accept an array

412
00:22:43.170 --> 00:22:44.880
and will give us the largest value.

413
00:22:44.880 --> 00:22:46.530
That's not how it works.

414
00:22:46.530 --> 00:22:49.860
The way this works is that we give it multiple values

415
00:22:49.860 --> 00:22:53.040
and it'll simply give us the largest of them.

416
00:22:53.040 --> 00:22:54.900
So we've done this before.

417
00:22:54.900 --> 00:22:56.730
Let me just demonstrate that to you

418
00:22:56.730 --> 00:23:00.510
so we can pass as many values as we want here.

419
00:23:00.510 --> 00:23:03.420
And of all of these arguments that we pass it,

420
00:23:03.420 --> 00:23:05.733
it'll just give us the largest ones.

421
00:23:06.870 --> 00:23:08.490
So how do we do this?

422
00:23:08.490 --> 00:23:11.820
Basically, how do we take the values out of the array?

423
00:23:11.820 --> 00:23:15.540
Well, that's where this spread operator comes into play,

424
00:23:15.540 --> 00:23:19.710
because what this operator does is basically just like this,

425
00:23:19.710 --> 00:23:23.100
it takes the values here and places them.

426
00:23:23.100 --> 00:23:24.750
Well, now it's gone,

427
00:23:24.750 --> 00:23:26.340
but it places them right there

428
00:23:26.340 --> 00:23:31.340
into this Math.max function here as arguments in this case.

429
00:23:32.190 --> 00:23:37.113
And so now indeed, we get 32 as the largest of these values.

430
00:23:38.190 --> 00:23:39.023
Great.

431
00:23:39.023 --> 00:23:43.290
So this was a lot of work, but I hope also a lot of fun.

432
00:23:43.290 --> 00:23:45.750
I really liked designing this challenge for you

433
00:23:45.750 --> 00:23:47.970
and now also solving it.

434
00:23:47.970 --> 00:23:51.480
So a lot of different methods we used here.

435
00:23:51.480 --> 00:23:54.870
And yeah, really nice challenge here as well.

436
00:23:54.870 --> 00:23:56.520
So I hope you had some fun,

437
00:23:56.520 --> 00:23:58.290
and I see you in the next video

438
00:23:58.290 --> 00:23:59.820
where we keep working

439
00:23:59.820 --> 00:24:02.043
with some very important array methods.

