Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
1
00:00:01,481 --> 00:00:03,730
(Instructor) Welcome to the final coding challenge
2
2
00:00:03,730 --> 00:00:05,430
of this section.
3
3
00:00:05,430 --> 00:00:07,710
And this one can really be seen
4
4
00:00:07,710 --> 00:00:10,750
as a final test of this section.
5
5
00:00:10,750 --> 00:00:12,140
So we're going to practice most
6
6
00:00:12,140 --> 00:00:16,373
of what we've been doing here and so let's get started.
7
7
00:00:18,250 --> 00:00:19,480
And in this challenge,
8
8
00:00:19,480 --> 00:00:22,870
we are back to Julia and Kate studying dogs.
9
9
00:00:22,870 --> 00:00:24,569
And this time they are studying
10
10
00:00:24,569 --> 00:00:27,963
whether dogs are eating too much or too little.
11
11
00:00:28,918 --> 00:00:33,650
So eating too much means that the dogs current food portion
12
12
00:00:33,650 --> 00:00:36,420
is larger than the recommended portion.
13
13
00:00:36,420 --> 00:00:39,570
And eating too little is the opposite.
14
14
00:00:39,570 --> 00:00:42,410
And then there's also eating an okay amount.
15
15
00:00:42,410 --> 00:00:45,930
And so that's when the dog's current food portion
16
16
00:00:45,930 --> 00:00:48,340
is within the range 10% above,
17
17
00:00:48,340 --> 00:00:51,370
and 10% below the recommended portion.
18
18
00:00:51,370 --> 00:00:54,463
And we will check that out in detail in a second.
19
19
00:00:55,719 --> 00:00:58,490
And so here are your tasks.
20
20
00:00:58,490 --> 00:01:00,890
And actually, each of them can be seen
21
21
00:01:00,890 --> 00:01:02,450
as a separate challenge.
22
22
00:01:02,450 --> 00:01:04,440
So there are eight of them.
23
23
00:01:04,440 --> 00:01:06,910
So this is going to be a long one.
24
24
00:01:06,910 --> 00:01:09,930
And some of them are actually a little bit tricky.
25
25
00:01:09,930 --> 00:01:14,860
So a bit hard to solve and that is one more time on purpose.
26
26
00:01:14,860 --> 00:01:17,580
So that you can actually challenge yourself,
27
27
00:01:17,580 --> 00:01:19,270
and try to do a little bit more
28
28
00:01:19,270 --> 00:01:22,300
than just what we learned here in these videos.
29
29
00:01:22,300 --> 00:01:25,510
And as always, if you cannot solve one of them on your own,
30
30
00:01:25,510 --> 00:01:27,960
then just go ahead and watch the solution,
31
31
00:01:27,960 --> 00:01:30,123
and then go back to the next point.
32
32
00:01:31,088 --> 00:01:34,930
Now anyway, let's get started here.
33
33
00:01:34,930 --> 00:01:37,690
And the first task is to loop over the array
34
34
00:01:37,690 --> 00:01:40,273
that contains the dog objects.
35
35
00:01:41,410 --> 00:01:43,540
So that is this object here.
36
36
00:01:43,540 --> 00:01:46,200
And this is the data that we're going to work with.
37
37
00:01:46,200 --> 00:01:48,390
So it's this array called dogs.
38
38
00:01:48,390 --> 00:01:50,400
And in each position of the array,
39
39
00:01:50,400 --> 00:01:52,823
we have one object per dog.
40
40
00:01:56,070 --> 00:01:59,550
So your task is to loop over that array.
41
41
00:01:59,550 --> 00:02:01,700
And then for each dog to calculate
42
42
00:02:01,700 --> 00:02:03,800
the recommended food portion,
43
43
00:02:03,800 --> 00:02:07,590
and then to add it to the object as a new property.
44
44
00:02:07,590 --> 00:02:09,800
So do not create a new array here,
45
45
00:02:09,800 --> 00:02:12,800
just loop over the array that we already have,
46
46
00:02:12,800 --> 00:02:16,960
and add the recommended food portion as a property.
47
47
00:02:16,960 --> 00:02:19,950
And so I hope you know which array method
48
48
00:02:19,950 --> 00:02:21,730
you need to use for that.
49
49
00:02:21,730 --> 00:02:24,920
And to calculate that recommended food portion,
50
50
00:02:24,920 --> 00:02:27,220
simply take the weight of the dog
51
51
00:02:27,220 --> 00:02:31,673
and elevate it to 0.75 times 28.
52
52
00:02:33,650 --> 00:02:37,850
Then next up, find Sarah's dog and log to the console,
53
53
00:02:37,850 --> 00:02:41,050
whether it is eating too much or too little.
54
54
00:02:41,050 --> 00:02:43,379
Now in this one, it's important to keep in mind,
55
55
00:02:43,379 --> 00:02:47,070
or important to notice that these owners
56
56
00:02:47,070 --> 00:02:50,490
are actually inside an array.
57
57
00:02:50,490 --> 00:02:55,490
And so this makes this task a little bit more tricky.
58
58
00:02:55,570 --> 00:02:58,003
And so again, that is on purpose.
59
59
00:02:59,280 --> 00:03:01,440
Next up, create an array containing
60
60
00:03:01,440 --> 00:03:05,160
all the owners of dogs who eat too much.
61
61
00:03:05,160 --> 00:03:08,350
So call this one owners eat too much
62
62
00:03:08,350 --> 00:03:10,590
and then an array with the opposite.
63
63
00:03:10,590 --> 00:03:14,110
So with owners of dogs who eat too little.
64
64
00:03:14,110 --> 00:03:16,360
Then log a string to the console
65
65
00:03:16,360 --> 00:03:21,360
for each of the arrays that we created previously like this.
66
66
00:03:21,730 --> 00:03:25,890
So Matilda, Alice and Bob's dogs eat too much.
67
67
00:03:25,890 --> 00:03:27,950
So that's based on this array.
68
68
00:03:27,950 --> 00:03:31,933
And then based on this array, a string like this.
69
69
00:03:33,241 --> 00:03:37,190
Then afterwards, log to the console,
70
70
00:03:37,190 --> 00:03:41,739
whether there is any dog eating an okay amount of food.
71
71
00:03:41,739 --> 00:03:43,760
And so let's take a look
72
72
00:03:43,760 --> 00:03:47,023
at what an okay amount of food actually means.
73
73
00:03:48,680 --> 00:03:50,490
So here we have to send two
74
74
00:03:50,490 --> 00:03:53,830
and so being within the range 10% above
75
75
00:03:53,830 --> 00:03:57,790
and 10% below means that the current food portion,
76
76
00:03:57,790 --> 00:04:00,450
which is stored here in this property
77
77
00:04:00,450 --> 00:04:03,320
should be greater than the recommended food portion
78
78
00:04:03,320 --> 00:04:07,090
that you calculated before times 0.9.
79
79
00:04:07,090 --> 00:04:10,220
So that's 90% and at the same time,
80
80
00:04:10,220 --> 00:04:14,041
it should be less than 110% basically,
81
81
00:04:14,041 --> 00:04:16,460
of the recommended amount.
82
82
00:04:16,460 --> 00:04:19,390
So these two conditions here at the same time
83
83
00:04:19,390 --> 00:04:22,523
mean that the dog is eating an okay amount of food.
84
84
00:04:23,380 --> 00:04:26,110
So basically, it's what we see here.
85
85
00:04:26,110 --> 00:04:28,640
So the current portion should be between 90
86
86
00:04:28,640 --> 00:04:31,803
and 110% of the recommended.
87
87
00:04:33,480 --> 00:04:36,240
Then number seven, create an array containing
88
88
00:04:36,240 --> 00:04:40,320
all the dogs that are eating an okay amount of food
89
89
00:04:40,320 --> 00:04:42,910
and I want you to reuse the condition
90
90
00:04:42,910 --> 00:04:45,463
that we created before here are a number of six.
91
91
00:04:46,680 --> 00:04:50,570
Finally, create a shallow copy of the dog's array
92
92
00:04:50,570 --> 00:04:53,780
and then sort it by the recommended food portion
93
93
00:04:53,780 --> 00:04:55,690
in an ascending order.
94
94
00:04:55,690 --> 00:04:56,860
Now just keep in mind
95
95
00:04:56,860 --> 00:05:00,393
that the portions are inside the arrays objects.
96
96
00:05:01,430 --> 00:05:03,683
Okay, so to sort this array,
97
97
00:05:03,683 --> 00:05:07,150
it's a little bit trickier than what we did before.
98
98
00:05:07,150 --> 00:05:12,120
Because yeah, the value is now here inside of an object,
99
99
00:05:12,120 --> 00:05:14,540
and not just in the array itself.
100
100
00:05:14,540 --> 00:05:15,690
But the way that we write
101
101
00:05:15,690 --> 00:05:18,100
the callback function is pretty similar.
102
102
00:05:18,100 --> 00:05:21,000
So we will still have the ANP parameters,
103
103
00:05:21,000 --> 00:05:22,730
but these are no objects.
104
104
00:05:22,730 --> 00:05:25,510
And so to compare the recommended food portion,
105
105
00:05:25,510 --> 00:05:27,042
you will have to get that property
106
106
00:05:27,042 --> 00:05:29,113
out of each of these objects.
107
107
00:05:30,040 --> 00:05:32,766
So yeah, that's it.
108
108
00:05:32,766 --> 00:05:35,390
It's probably going to take you some time.
109
109
00:05:35,390 --> 00:05:36,890
But what matters most
110
110
00:05:36,890 --> 00:05:40,830
is that you practice and that you don't give up.
111
111
00:05:40,830 --> 00:05:43,700
But anyway, let's take now like half an hour,
112
112
00:05:43,700 --> 00:05:46,377
or even one hour to solve this one
113
113
00:05:46,377 --> 00:05:48,083
and then I see you back here.
114
114
00:05:51,722 --> 00:05:56,170
So let's get started with the solution.
115
115
00:05:56,170 --> 00:05:58,060
So the first one is to calculate
116
116
00:05:58,060 --> 00:06:00,963
the recommended food for each of the dog.
117
117
00:06:01,940 --> 00:06:05,300
Now, as I said, you should not create a new array,
118
118
00:06:05,300 --> 00:06:08,890
but simply loop over the already existing array.
119
119
00:06:08,890 --> 00:06:12,960
So this dogs array, actually copy it here.
120
120
00:06:12,960 --> 00:06:13,873
So copy.
121
121
00:06:17,500 --> 00:06:21,420
So looping over this array and then to each object,
122
122
00:06:21,420 --> 00:06:24,100
we want to add a new property.
123
123
00:06:24,100 --> 00:06:27,083
And so the for each method is the way to go here.
124
124
00:06:29,120 --> 00:06:31,653
Then each element is a dog.
125
125
00:06:32,500 --> 00:06:36,630
And what we want to do is to add a property.
126
126
00:06:36,630 --> 00:06:41,290
So dog.recommended food is equal
127
127
00:06:42,147 --> 00:06:47,147
to dog.weight elevated 0.75 times 28.
128
128
00:06:53,240 --> 00:06:55,303
And now let's take a look at the dogs.
129
129
00:07:00,280 --> 00:07:02,630
And indeed, now they have the recommended food.
130
130
00:07:03,600 --> 00:07:07,320
Now let's just get rid of this decimal part here.
131
131
00:07:07,320 --> 00:07:12,320
So let's say here, math.trunc.
132
132
00:07:14,080 --> 00:07:17,513
So we use this one before we simply truncate.
133
133
00:07:18,530 --> 00:07:21,210
So we cut the decimal parts.
134
134
00:07:21,210 --> 00:07:25,493
And so that's number one and now number two,
135
135
00:07:26,940 --> 00:07:28,323
let's check it out again.
136
136
00:07:29,750 --> 00:07:33,640
So we need to find Sarah's dog and then log to the console,
137
137
00:07:33,640 --> 00:07:37,070
whether it's eating too much or too little.
138
138
00:07:37,070 --> 00:07:40,653
So basically, we need to find the object here.
139
139
00:07:42,630 --> 00:07:45,120
So in this case, that's going to be this one.
140
140
00:07:45,120 --> 00:07:47,040
So we need to find this object,
141
141
00:07:47,040 --> 00:07:49,290
and then compare the recommended food
142
142
00:07:49,290 --> 00:07:52,363
we just calculated with the current food.
143
143
00:07:53,620 --> 00:07:58,143
So the trickiest part here is going to be to find the owner.
144
144
00:07:59,270 --> 00:08:01,660
But I believe we can do it.
145
145
00:08:01,660 --> 00:08:06,660
So let's call this one dogSarah, then it's dogs.
146
146
00:08:08,310 --> 00:08:10,050
And we use the find method,
147
147
00:08:10,050 --> 00:08:12,180
because we want to find one element
148
148
00:08:12,180 --> 00:08:15,350
of the array based on a condition.
149
149
00:08:15,350 --> 00:08:18,750
So in each iteration, we have one dog.
150
150
00:08:18,750 --> 00:08:23,340
And now we want to take dog.owners.
151
151
00:08:23,340 --> 00:08:27,130
And now this itself is also an array.
152
152
00:08:27,130 --> 00:08:28,483
So it is this here.
153
153
00:08:29,670 --> 00:08:34,540
And what we want now is for this array to include Sarah.
154
154
00:08:34,540 --> 00:08:39,540
And so we can do this, includes Sarah and that's it.
155
155
00:08:41,880 --> 00:08:45,270
This is how we get our true or false value.
156
156
00:08:45,270 --> 00:08:48,980
So remember that in the callback of the find method,
157
157
00:08:48,980 --> 00:08:52,090
we need to return a true or a false value.
158
158
00:08:52,090 --> 00:08:53,930
And then the find method will return
159
159
00:08:53,930 --> 00:08:57,460
the first element for which the condition is true.
160
160
00:08:57,460 --> 00:09:00,930
And in this case, we get the true or false value
161
161
00:09:00,930 --> 00:09:05,360
by calling the includes method on the owners array.
162
162
00:09:05,360 --> 00:09:09,180
And so when the array of owners includes Sarah,
163
163
00:09:09,180 --> 00:09:10,880
then that condition is true.
164
164
00:09:10,880 --> 00:09:13,313
And then we are going to get this object.
165
165
00:09:14,660 --> 00:09:18,080
So let's just make sure here and let's see.
166
166
00:09:22,590 --> 00:09:25,423
And indeed, it is the correct object.
167
167
00:09:26,610 --> 00:09:28,113
We need some more space here.
168
168
00:09:29,410 --> 00:09:31,320
This we don't need any more.
169
169
00:09:31,320 --> 00:09:33,270
And now we just need to log to the console
170
170
00:09:33,270 --> 00:09:36,690
whether the dog is eating too much or not.
171
171
00:09:36,690 --> 00:09:39,310
And we already know that it's eating too much
172
172
00:09:39,310 --> 00:09:43,827
because the current food is 275 and the recommended is 191.
173
173
00:09:46,430 --> 00:09:51,430
So let's log Sarah's dog is eating.
174
174
00:09:54,960 --> 00:09:57,090
And here I'm going to use my old friend,
175
175
00:09:57,090 --> 00:10:01,967
the ternary operator, Sarah.current food.
176
176
00:10:03,350 --> 00:10:08,253
If it's greater, then dogSarah.recommended food.
177
177
00:10:09,900 --> 00:10:14,670
Well then here we want to have much,
178
178
00:10:14,670 --> 00:10:15,703
because in this situation,
179
179
00:10:15,703 --> 00:10:18,440
the dog is clearly eating too much.
180
180
00:10:18,440 --> 00:10:22,423
Now of course this needs to be here inside the curly braces.
181
181
00:10:25,440 --> 00:10:28,830
And here now it's correct.
182
182
00:10:28,830 --> 00:10:31,200
So we're asking basically this question.
183
183
00:10:31,200 --> 00:10:34,000
So if the current food is more than the recommended,
184
184
00:10:34,000 --> 00:10:39,000
then the dog is eating too much and else too little.
185
185
00:10:41,720 --> 00:10:45,690
And so indeed, Sarah's dog is eating too much.
186
186
00:10:45,690 --> 00:10:48,000
So that's the conclusion we had already reached
187
187
00:10:48,000 --> 00:10:49,880
by looking here at the numbers.
188
188
00:10:49,880 --> 00:10:51,253
Okay, moving on.
189
189
00:10:52,970 --> 00:10:57,970
Let's take a second here to look at the exercise again.
190
190
00:10:58,620 --> 00:11:00,270
So create an array containing
191
191
00:11:00,270 --> 00:11:03,930
all owners of dogs who eat too much.
192
192
00:11:03,930 --> 00:11:05,830
So let's copy this name.
193
193
00:11:05,830 --> 00:11:08,923
And then also one where they eat too little.
194
194
00:11:13,767 --> 00:11:15,363
So how are we going to do that?
195
195
00:11:17,150 --> 00:11:19,620
Well, we know that we want a new array
196
196
00:11:19,620 --> 00:11:23,136
based on the existing dog's array.
197
197
00:11:23,136 --> 00:11:28,136
And we want to create this new array based on a condition.
198
198
00:11:28,170 --> 00:11:31,210
So once again, we want to compare the current food
199
199
00:11:31,210 --> 00:11:33,426
to the recommended food.
200
200
00:11:33,426 --> 00:11:38,053
And therefore the correct method to use here is the filter.
201
201
00:11:40,246 --> 00:11:43,850
So in each iteration, we get a dog.
202
202
00:11:43,850 --> 00:11:48,405
And we're going to return whether this dog's current food
203
203
00:11:48,405 --> 00:11:53,405
is greater than the dog recommended food.
204
204
00:11:55,030 --> 00:11:59,490
Because here we are looking for the dogs that eat too much.
205
205
00:11:59,490 --> 00:12:03,230
And so that means that the current food
206
206
00:12:03,230 --> 00:12:04,893
is greater than the recommended.
207
207
00:12:06,353 --> 00:12:09,810
So this will give us the dogs.
208
208
00:12:09,810 --> 00:12:14,700
But let's start by taking a look at this variable for now,
209
209
00:12:14,700 --> 00:12:16,250
even though we want the owners.
210
210
00:12:20,900 --> 00:12:24,936
So this one doesn't show it to us.
211
211
00:12:24,936 --> 00:12:28,510
But yes, so this dog here
212
212
00:12:28,510 --> 00:12:30,333
is eating more than the recommended.
213
213
00:12:31,385 --> 00:12:36,385
Now, we now want to get the owner or actually the owners.
214
214
00:12:38,700 --> 00:12:41,210
So again, we want to create a new array
215
215
00:12:41,210 --> 00:12:43,400
based on an existing array.
216
216
00:12:43,400 --> 00:12:45,200
And this time, this new array should
217
217
00:12:45,200 --> 00:12:47,490
have the same length as before.
218
218
00:12:47,490 --> 00:12:50,710
We simply want to take something out of the original array
219
219
00:12:50,710 --> 00:12:53,090
and put it into the new array.
220
220
00:12:53,090 --> 00:12:56,133
And so that means we're going to use map.
221
221
00:12:57,110 --> 00:12:59,450
So once again, we have a dog
222
222
00:12:59,450 --> 00:13:04,312
and we want to return the dog.owners.
223
223
00:13:04,312 --> 00:13:07,560
And now we have an array of arrays,
224
224
00:13:07,560 --> 00:13:11,580
where one is Matilda, and the other one is Sarah and John.
225
225
00:13:11,580 --> 00:13:13,700
And so finally, the next step,
226
226
00:13:13,700 --> 00:13:17,060
to transform all of this into just one array,
227
227
00:13:17,060 --> 00:13:18,823
is to call flat.
228
228
00:13:20,554 --> 00:13:25,210
And so indeed, we end up with this array
229
229
00:13:25,210 --> 00:13:26,620
with just one level.
230
230
00:13:26,620 --> 00:13:30,420
And with all the owners of all the dogs that eat too much.
231
231
00:13:30,420 --> 00:13:33,460
So great and maybe you can see here
232
232
00:13:33,460 --> 00:13:35,880
that we can actually do even better,
233
233
00:13:35,880 --> 00:13:39,550
because whenever we have a map combined with a flat,
234
234
00:13:39,550 --> 00:13:42,650
we can instead use flat map.
235
235
00:13:42,650 --> 00:13:47,650
So let's transform this year into flat map.
236
236
00:13:48,380 --> 00:13:50,552
And then we don't even need this part
237
237
00:13:50,552 --> 00:13:53,303
and the result is the same indeed.
238
238
00:13:55,536 --> 00:14:00,220
And based on this one, we can easily create the owners
239
239
00:14:01,390 --> 00:14:03,573
where the dogs eat too little.
240
240
00:14:05,920 --> 00:14:09,423
So all we need to do is to invert this sign.
241
241
00:14:10,880 --> 00:14:12,860
Let's get rid of this one here.
242
242
00:14:12,860 --> 00:14:15,713
And so these are the other three owners basically.
243
243
00:14:17,827 --> 00:14:22,827
Next up, our task is to create some strings.
244
244
00:14:22,930 --> 00:14:24,663
So let me just copy the strings.
245
245
00:14:28,070 --> 00:14:31,703
And so that's based on the arrays that we just created.
246
246
00:14:41,620 --> 00:14:43,163
So these are the two strings.
247
247
00:14:44,090 --> 00:14:46,100
And actually they will not be the same.
248
248
00:14:46,100 --> 00:14:47,950
So this is just an example.
249
249
00:14:47,950 --> 00:14:52,140
So here we see Alice, Bob and Michael's dogs are together,
250
250
00:14:52,140 --> 00:14:53,990
and here they are not.
251
251
00:14:53,990 --> 00:14:56,840
So the string will be different, but that doesn't matter.
252
252
00:14:59,480 --> 00:15:04,480
So what matters is that here we have dogs eat too much.
253
253
00:15:05,650 --> 00:15:09,940
And so now all we do, is to take the owners
254
254
00:15:09,940 --> 00:15:11,943
that we already have eat too much.
255
255
00:15:13,170 --> 00:15:15,410
And essentially join them together
256
256
00:15:15,410 --> 00:15:19,393
using the separator string of and, like this.
257
257
00:15:21,240 --> 00:15:23,140
And so now we get this array
258
258
00:15:23,140 --> 00:15:27,180
here Matilda, Sarah and John dog.
259
259
00:15:27,180 --> 00:15:32,180
So let's just add this eat too much.
260
260
00:15:34,530 --> 00:15:38,652
And now the other way around eat too little
261
261
00:15:38,652 --> 00:15:42,483
and here too little as well.
262
262
00:15:43,900 --> 00:15:44,743
Beautiful.
263
263
00:15:47,810 --> 00:15:50,120
So we're making good progress here,
264
264
00:15:50,120 --> 00:15:51,803
let's move on to number five.
265
265
00:15:52,660 --> 00:15:54,943
So log whether there is any dog eating exactly
266
266
00:15:54,943 --> 00:15:58,970
the amount of food that is recommended.
267
267
00:15:58,970 --> 00:16:01,240
So whenever you see the word any,
268
268
00:16:01,240 --> 00:16:04,093
you can think of the sum method.
269
269
00:16:05,340 --> 00:16:06,963
So remember that the sum method
270
270
00:16:06,963 --> 00:16:10,520
will return true if at least one of the elements
271
271
00:16:10,520 --> 00:16:13,323
in the array satisfies a certain condition.
272
272
00:16:14,580 --> 00:16:19,053
So log to the console, dogs.sum.
273
273
00:16:23,040 --> 00:16:24,830
And so one more time,
274
274
00:16:24,830 --> 00:16:29,830
we are comparing the current food with the recommended food.
275
275
00:16:30,360 --> 00:16:34,320
And this time, we want to know if there is at least one dog
276
276
00:16:34,320 --> 00:16:38,560
eating exactly the same amount as the recommended portion.
277
277
00:16:38,560 --> 00:16:41,320
And so we use the equal operator here.
278
278
00:16:41,320 --> 00:16:44,993
So current and recommended food.
279
279
00:16:46,700 --> 00:16:48,533
And well, there is none.
280
280
00:16:49,580 --> 00:16:52,793
Let's take a look at our dogs here just to confirm that.
281
281
00:16:57,430 --> 00:17:00,053
So these are different, these are different,
282
282
00:17:01,150 --> 00:17:05,090
these are different and also probably yeah,
283
283
00:17:05,090 --> 00:17:07,120
this is also different.
284
284
00:17:07,120 --> 00:17:10,240
And therefore, if all of them are false,
285
285
00:17:10,240 --> 00:17:13,473
then the sum method also returns false.
286
286
00:17:17,360 --> 00:17:20,100
Number six, log to the console,
287
287
00:17:20,100 --> 00:17:23,533
whether there is any dog eating an okay amount of food.
288
288
00:17:24,957 --> 00:17:28,780
And so now this formula here comes into play.
289
289
00:17:28,780 --> 00:17:32,003
Let's copy it just so we see it down here.
290
290
00:17:35,930 --> 00:17:37,567
And so once again,
291
291
00:17:37,567 --> 00:17:41,790
the goal is to simply return true or false,
292
292
00:17:41,790 --> 00:17:44,150
whether there is at least one dog
293
293
00:17:44,150 --> 00:17:46,580
eating an okay amount of food.
294
294
00:17:46,580 --> 00:17:48,313
So we need the sum method again.
295
295
00:17:49,490 --> 00:17:53,430
And now this a little bit more complex condition.
296
296
00:17:53,430 --> 00:17:57,410
So dog.current food portion
297
297
00:17:57,410 --> 00:18:02,317
needs to be greater than the recommended times 0.9.
298
298
00:18:07,240 --> 00:18:11,203
So that's 90% and then at the same time,
299
299
00:18:12,730 --> 00:18:16,313
so this, (laughs) now let me just copy it here.
300
300
00:18:19,010 --> 00:18:20,900
So at the same time,
301
301
00:18:20,900 --> 00:18:24,563
the dog should also be eating less than 110%
302
302
00:18:27,977 --> 00:18:29,323
of the recommended amount.
303
303
00:18:30,980 --> 00:18:33,230
And so now we get true.
304
304
00:18:33,230 --> 00:18:37,730
So there is at least one dog but we don't know how many.
305
305
00:18:37,730 --> 00:18:41,380
And so that's the task basically of the next point,
306
306
00:18:41,380 --> 00:18:43,610
which is to filter out all the dogs
307
307
00:18:43,610 --> 00:18:47,570
that are actually eating this okay amount of food.
308
308
00:18:47,570 --> 00:18:49,490
And for that we should also export
309
309
00:18:49,490 --> 00:18:52,063
this condition into a separate function.
310
310
00:18:52,950 --> 00:18:55,565
So this is really just a function.
311
311
00:18:55,565 --> 00:19:00,163
So we can take it and store it into its own variable.
312
312
00:19:01,120 --> 00:19:05,390
And let's call it check eating okay.
313
313
00:19:08,938 --> 00:19:10,570
And then simply the function.
314
314
00:19:10,570 --> 00:19:12,763
And so here, we can then use that function.
315
315
00:19:15,030 --> 00:19:18,280
And yeah, we still get the same result.
316
316
00:19:18,280 --> 00:19:21,840
And now as I was just saying,
317
317
00:19:21,840 --> 00:19:24,240
the goal here is to create an array
318
318
00:19:24,240 --> 00:19:27,020
containing only the dogs that are eating
319
319
00:19:27,020 --> 00:19:28,843
an okay amount of food.
320
320
00:19:30,240 --> 00:19:34,740
So we want a new array based on an existing array,
321
321
00:19:34,740 --> 00:19:37,000
but only with some of the elements.
322
322
00:19:37,000 --> 00:19:41,163
And so it is time again for using the filter method.
323
323
00:19:42,810 --> 00:19:47,690
So docs dot filter and now this is very easy,
324
324
00:19:47,690 --> 00:19:50,707
because we already have this condition
325
325
00:19:50,707 --> 00:19:53,323
callback function here in this variable.
326
326
00:19:55,740 --> 00:20:00,040
And well, we only have one dog in this condition as you see.
327
327
00:20:00,040 --> 00:20:01,667
So that's Michael's dog,
328
328
00:20:01,667 --> 00:20:05,460
where the recommended food is indeed pretty close
329
329
00:20:05,460 --> 00:20:07,318
to the current food.
330
330
00:20:07,318 --> 00:20:10,310
And so we consider that this is an okay amount of food
331
331
00:20:10,310 --> 00:20:12,203
to eat for the dog.
332
332
00:20:13,190 --> 00:20:15,200
Okay, almost ready here.
333
333
00:20:15,200 --> 00:20:17,375
So I told you this is a long one.
334
334
00:20:17,375 --> 00:20:20,320
But I hope you're having fun with this one.
335
335
00:20:20,320 --> 00:20:23,040
This kind of seems like a real world challenge.
336
336
00:20:23,040 --> 00:20:26,130
So like a real world task, doesn't it?
337
337
00:20:26,130 --> 00:20:28,950
So it took quite some time and designing this challenge.
338
338
00:20:28,950 --> 00:20:32,000
And even that was also a lot of fun.
339
339
00:20:32,000 --> 00:20:36,660
But anyway, let's now create a copy of the dogs array,
340
340
00:20:36,660 --> 00:20:39,823
and then sorted by the recommended food portion.
341
341
00:20:41,660 --> 00:20:44,060
So let me actually copy this
342
342
00:20:44,060 --> 00:20:47,573
so we don't forget what we have to do here.
343
343
00:20:51,070 --> 00:20:53,523
So we start by creating a copy.
344
344
00:20:55,020 --> 00:21:00,020
So dogs.copy, no dot of course,
345
345
00:21:00,810 --> 00:21:02,950
just the name is dogs copy.
346
346
00:21:02,950 --> 00:21:07,950
And so let's use dogs.slice for that.
347
347
00:21:08,270 --> 00:21:10,930
And actually, we can call the sort method
348
348
00:21:10,930 --> 00:21:12,433
then immediately on that.
349
349
00:21:13,546 --> 00:21:17,660
So here, we are going to need a callback function as always,
350
350
00:21:17,660 --> 00:21:19,470
which is the sorting function.
351
351
00:21:19,470 --> 00:21:24,033
And this always has a and b as the parameters.
352
352
00:21:25,050 --> 00:21:28,410
So we could call them something else of course here,
353
353
00:21:28,410 --> 00:21:31,390
but I like to always call them a and b.
354
354
00:21:31,390 --> 00:21:33,966
And actually, we don't even need this block here.
355
355
00:21:33,966 --> 00:21:35,860
So remember that before,
356
356
00:21:35,860 --> 00:21:39,450
when we wanted to sort an array in an ascending order,
357
357
00:21:39,450 --> 00:21:43,800
we simply would do a minus b like this.
358
358
00:21:43,800 --> 00:21:47,893
But this was when we had an array, simply like this.
359
359
00:21:49,390 --> 00:21:50,710
So just with numbers,
360
360
00:21:50,710 --> 00:21:53,750
but now we have these numbers here in objects.
361
361
00:21:53,750 --> 00:21:56,503
And so a and b, are now the objects.
362
362
00:21:57,360 --> 00:22:00,450
However, the values that we want to subtract
363
363
00:22:00,450 --> 00:22:02,840
are in these objects and we can simply
364
364
00:22:02,840 --> 00:22:03,940
get them out of there.
365
365
00:22:05,500 --> 00:22:08,570
So we want to sort by the recommended food portion.
366
366
00:22:08,570 --> 00:22:10,923
And so let's simply do this.
367
367
00:22:16,590 --> 00:22:18,263
And that's actually it.
368
368
00:22:19,507 --> 00:22:21,643
Let's call this your dog's sorted.
369
369
00:22:24,615 --> 00:22:28,760
And then let's log that to the console here.
370
370
00:22:29,750 --> 00:22:31,203
Dog's sorted.
371
371
00:22:32,810 --> 00:22:35,543
Let's see and yeah, that worked.
372
372
00:22:36,690 --> 00:22:38,523
Oh, actually we cannot see it yet.
373
373
00:22:41,640 --> 00:22:45,060
So yeah, here is the recommended food.
374
374
00:22:45,060 --> 00:22:49,400
So we start with the lowest one, then 191.
375
375
00:22:49,400 --> 00:22:52,440
And you see that indeed, it is in an ascending order.
376
376
00:22:52,440 --> 00:22:55,017
So 284 and 376.
377
377
00:22:59,150 --> 00:23:00,680
So that worked.
378
378
00:23:00,680 --> 00:23:03,802
And so we finished this coding challenge.
379
379
00:23:03,802 --> 00:23:06,110
This was a lot of work.
380
380
00:23:06,110 --> 00:23:09,270
And as I just said, also a lot of fun.
381
381
00:23:09,270 --> 00:23:10,788
And I hope you felt the same.
382
382
00:23:10,788 --> 00:23:15,480
And also that you felt the same for this entire section.
383
383
00:23:15,480 --> 00:23:18,270
So this project was a great project in my opinion,
384
384
00:23:18,270 --> 00:23:21,376
it was a lot of fun to build it together with you,
385
385
00:23:21,376 --> 00:23:25,600
and to put it together and design it in the first place.
386
386
00:23:25,600 --> 00:23:28,440
So I hope you're proud of what you achieved here.
387
387
00:23:28,440 --> 00:23:31,290
And one more time I want to congratulate you
388
388
00:23:31,290 --> 00:23:34,250
for making it this far and still sticking
389
389
00:23:34,250 --> 00:23:37,190
with this quite long course.
390
390
00:23:37,190 --> 00:23:39,830
Anyway with that, I see you in the next section,
391
391
00:23:39,830 --> 00:23:41,960
where we will actually continue building
392
392
00:23:41,960 --> 00:23:45,463
on this project by adding a couple of new things to it.
34301
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.