Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
(Instructor) Welcome to the final coding challenge 2
2
of this section. 3
3
And this one can really be seen 4
4
as a final test of this section. 5
5
So we're going to practice most 6
6
of what we've been doing here and so let's get started. 7
7
And in this challenge, 8
8
we are back to Julia and Kate studying dogs. 9
9
And this time they are studying 10
10
whether dogs are eating too much or too little. 11
11
So eating too much means that the dogs current food portion 12
12
is larger than the recommended portion. 13
13
And eating too little is the opposite. 14
14
And then there's also eating an okay amount. 15
15
And so that's when the dog's current food portion 16
16
is within the range 10% above, 17
17
and 10% below the recommended portion. 18
18
And we will check that out in detail in a second. 19
19
And so here are your tasks. 20
20
And actually, each of them can be seen 21
21
as a separate challenge. 22
22
So there are eight of them. 23
23
So this is going to be a long one. 24
24
And some of them are actually a little bit tricky. 25
25
So a bit hard to solve and that is one more time on purpose. 26
26
So that you can actually challenge yourself, 27
27
and try to do a little bit more 28
28
than just what we learned here in these videos. 29
29
And as always, if you cannot solve one of them on your own, 30
30
then just go ahead and watch the solution, 31
31
and then go back to the next point. 32
32
Now anyway, let's get started here. 33
33
And the first task is to loop over the array 34
34
that contains the dog objects. 35
35
So that is this object here. 36
36
And this is the data that we're going to work with. 37
37
So it's this array called dogs. 38
38
And in each position of the array, 39
39
we have one object per dog. 40
40
So your task is to loop over that array. 41
41
And then for each dog to calculate 42
42
the recommended food portion, 43
43
and then to add it to the object as a new property. 44
44
So do not create a new array here, 45
45
just loop over the array that we already have, 46
46
and add the recommended food portion as a property. 47
47
And so I hope you know which array method 48
48
you need to use for that. 49
49
And to calculate that recommended food portion, 50
50
simply take the weight of the dog 51
51
and elevate it to 0.75 times 28. 52
52
Then next up, find Sarah's dog and log to the console, 53
53
whether it is eating too much or too little. 54
54
Now in this one, it's important to keep in mind, 55
55
or important to notice that these owners 56
56
are actually inside an array. 57
57
And so this makes this task a little bit more tricky. 58
58
And so again, that is on purpose. 59
59
Next up, create an array containing 60
60
all the owners of dogs who eat too much. 61
61
So call this one owners eat too much 62
62
and then an array with the opposite. 63
63
So with owners of dogs who eat too little. 64
64
Then log a string to the console 65
65
for each of the arrays that we created previously like this. 66
66
So Matilda, Alice and Bob's dogs eat too much. 67
67
So that's based on this array. 68
68
And then based on this array, a string like this. 69
69
Then afterwards, log to the console, 70
70
whether there is any dog eating an okay amount of food. 71
71
And so let's take a look 72
72
at what an okay amount of food actually means. 73
73
So here we have to send two 74
74
and so being within the range 10% above 75
75
and 10% below means that the current food portion, 76
76
which is stored here in this property 77
77
should be greater than the recommended food portion 78
78
that you calculated before times 0.9. 79
79
So that's 90% and at the same time, 80
80
it should be less than 110% basically, 81
81
of the recommended amount. 82
82
So these two conditions here at the same time 83
83
mean that the dog is eating an okay amount of food. 84
84
So basically, it's what we see here. 85
85
So the current portion should be between 90 86
86
and 110% of the recommended. 87
87
Then number seven, create an array containing 88
88
all the dogs that are eating an okay amount of food 89
89
and I want you to reuse the condition 90
90
that we created before here are a number of six. 91
91
Finally, create a shallow copy of the dog's array 92
92
and then sort it by the recommended food portion 93
93
in an ascending order. 94
94
Now just keep in mind 95
95
that the portions are inside the arrays objects. 96
96
Okay, so to sort this array, 97
97
it's a little bit trickier than what we did before. 98
98
Because yeah, the value is now here inside of an object, 99
99
and not just in the array itself. 100
100
But the way that we write 101
101
the callback function is pretty similar. 102
102
So we will still have the ANP parameters, 103
103
but these are no objects. 104
104
And so to compare the recommended food portion, 105
105
you will have to get that property 106
106
out of each of these objects. 107
107
So yeah, that's it. 108
108
It's probably going to take you some time. 109
109
But what matters most 110
110
is that you practice and that you don't give up. 111
111
But anyway, let's take now like half an hour, 112
112
or even one hour to solve this one 113
113
and then I see you back here. 114
114
So let's get started with the solution. 115
115
So the first one is to calculate 116
116
the recommended food for each of the dog. 117
117
Now, as I said, you should not create a new array, 118
118
but simply loop over the already existing array. 119
119
So this dogs array, actually copy it here. 120
120
So copy. 121
121
So looping over this array and then to each object, 122
122
we want to add a new property. 123
123
And so the for each method is the way to go here. 124
124
Then each element is a dog. 125
125
And what we want to do is to add a property. 126
126
So dog.recommended food is equal 127
127
to dog.weight elevated 0.75 times 28. 128
128
And now let's take a look at the dogs. 129
129
And indeed, now they have the recommended food. 130
130
Now let's just get rid of this decimal part here. 131
131
So let's say here, math.trunc. 132
132
So we use this one before we simply truncate. 133
133
So we cut the decimal parts. 134
134
And so that's number one and now number two, 135
135
let's check it out again. 136
136
So we need to find Sarah's dog and then log to the console, 137
137
whether it's eating too much or too little. 138
138
So basically, we need to find the object here. 139
139
So in this case, that's going to be this one. 140
140
So we need to find this object, 141
141
and then compare the recommended food 142
142
we just calculated with the current food. 143
143
So the trickiest part here is going to be to find the owner. 144
144
But I believe we can do it. 145
145
So let's call this one dogSarah, then it's dogs. 146
146
And we use the find method, 147
147
because we want to find one element 148
148
of the array based on a condition. 149
149
So in each iteration, we have one dog. 150
150
And now we want to take dog.owners. 151
151
And now this itself is also an array. 152
152
So it is this here. 153
153
And what we want now is for this array to include Sarah. 154
154
And so we can do this, includes Sarah and that's it. 155
155
This is how we get our true or false value. 156
156
So remember that in the callback of the find method, 157
157
we need to return a true or a false value. 158
158
And then the find method will return 159
159
the first element for which the condition is true. 160
160
And in this case, we get the true or false value 161
161
by calling the includes method on the owners array. 162
162
And so when the array of owners includes Sarah, 163
163
then that condition is true. 164
164
And then we are going to get this object. 165
165
So let's just make sure here and let's see. 166
166
And indeed, it is the correct object. 167
167
We need some more space here. 168
168
This we don't need any more. 169
169
And now we just need to log to the console 170
170
whether the dog is eating too much or not. 171
171
And we already know that it's eating too much 172
172
because the current food is 275 and the recommended is 191. 173
173
So let's log Sarah's dog is eating. 174
174
And here I'm going to use my old friend, 175
175
the ternary operator, Sarah.current food. 176
176
If it's greater, then dogSarah.recommended food. 177
177
Well then here we want to have much, 178
178
because in this situation, 179
179
the dog is clearly eating too much. 180
180
Now of course this needs to be here inside the curly braces. 181
181
And here now it's correct. 182
182
So we're asking basically this question. 183
183
So if the current food is more than the recommended, 184
184
then the dog is eating too much and else too little. 185
185
And so indeed, Sarah's dog is eating too much. 186
186
So that's the conclusion we had already reached 187
187
by looking here at the numbers. 188
188
Okay, moving on. 189
189
Let's take a second here to look at the exercise again. 190
190
So create an array containing 191
191
all owners of dogs who eat too much. 192
192
So let's copy this name. 193
193
And then also one where they eat too little. 194
194
So how are we going to do that? 195
195
Well, we know that we want a new array 196
196
based on the existing dog's array. 197
197
And we want to create this new array based on a condition. 198
198
So once again, we want to compare the current food 199
199
to the recommended food. 200
200
And therefore the correct method to use here is the filter. 201
201
So in each iteration, we get a dog. 202
202
And we're going to return whether this dog's current food 203
203
is greater than the dog recommended food. 204
204
Because here we are looking for the dogs that eat too much. 205
205
And so that means that the current food 206
206
is greater than the recommended. 207
207
So this will give us the dogs. 208
208
But let's start by taking a look at this variable for now, 209
209
even though we want the owners. 210
210
So this one doesn't show it to us. 211
211
But yes, so this dog here 212
212
is eating more than the recommended. 213
213
Now, we now want to get the owner or actually the owners. 214
214
So again, we want to create a new array 215
215
based on an existing array. 216
216
And this time, this new array should 217
217
have the same length as before. 218
218
We simply want to take something out of the original array 219
219
and put it into the new array. 220
220
And so that means we're going to use map. 221
221
So once again, we have a dog 222
222
and we want to return the dog.owners. 223
223
And now we have an array of arrays, 224
224
where one is Matilda, and the other one is Sarah and John. 225
225
And so finally, the next step, 226
226
to transform all of this into just one array, 227
227
is to call flat. 228
228
And so indeed, we end up with this array 229
229
with just one level. 230
230
And with all the owners of all the dogs that eat too much. 231
231
So great and maybe you can see here 232
232
that we can actually do even better, 233
233
because whenever we have a map combined with a flat, 234
234
we can instead use flat map. 235
235
So let's transform this year into flat map. 236
236
And then we don't even need this part 237
237
and the result is the same indeed. 238
238
And based on this one, we can easily create the owners 239
239
where the dogs eat too little. 240
240
So all we need to do is to invert this sign. 241
241
Let's get rid of this one here. 242
242
And so these are the other three owners basically. 243
243
Next up, our task is to create some strings. 244
244
So let me just copy the strings. 245
245
And so that's based on the arrays that we just created. 246
246
So these are the two strings. 247
247
And actually they will not be the same. 248
248
So this is just an example. 249
249
So here we see Alice, Bob and Michael's dogs are together, 250
250
and here they are not. 251
251
So the string will be different, but that doesn't matter. 252
252
So what matters is that here we have dogs eat too much. 253
253
And so now all we do, is to take the owners 254
254
that we already have eat too much. 255
255
And essentially join them together 256
256
using the separator string of and, like this. 257
257
And so now we get this array 258
258
here Matilda, Sarah and John dog. 259
259
So let's just add this eat too much. 260
260
And now the other way around eat too little 261
261
and here too little as well. 262
262
Beautiful. 263
263
So we're making good progress here, 264
264
let's move on to number five. 265
265
So log whether there is any dog eating exactly 266
266
the amount of food that is recommended. 267
267
So whenever you see the word any, 268
268
you can think of the sum method. 269
269
So remember that the sum method 270
270
will return true if at least one of the elements 271
271
in the array satisfies a certain condition. 272
272
So log to the console, dogs.sum. 273
273
And so one more time, 274
274
we are comparing the current food with the recommended food. 275
275
And this time, we want to know if there is at least one dog 276
276
eating exactly the same amount as the recommended portion. 277
277
And so we use the equal operator here. 278
278
So current and recommended food. 279
279
And well, there is none. 280
280
Let's take a look at our dogs here just to confirm that. 281
281
So these are different, these are different, 282
282
these are different and also probably yeah, 283
283
this is also different. 284
284
And therefore, if all of them are false, 285
285
then the sum method also returns false. 286
286
Number six, log to the console, 287
287
whether there is any dog eating an okay amount of food. 288
288
And so now this formula here comes into play. 289
289
Let's copy it just so we see it down here. 290
290
And so once again, 291
291
the goal is to simply return true or false, 292
292
whether there is at least one dog 293
293
eating an okay amount of food. 294
294
So we need the sum method again. 295
295
And now this a little bit more complex condition. 296
296
So dog.current food portion 297
297
needs to be greater than the recommended times 0.9. 298
298
So that's 90% and then at the same time, 299
299
so this, (laughs) now let me just copy it here. 300
300
So at the same time, 301
301
the dog should also be eating less than 110% 302
302
of the recommended amount. 303
303
And so now we get true. 304
304
So there is at least one dog but we don't know how many. 305
305
And so that's the task basically of the next point, 306
306
which is to filter out all the dogs 307
307
that are actually eating this okay amount of food. 308
308
And for that we should also export 309
309
this condition into a separate function. 310
310
So this is really just a function. 311
311
So we can take it and store it into its own variable. 312
312
And let's call it check eating okay. 313
313
And then simply the function. 314
314
And so here, we can then use that function. 315
315
And yeah, we still get the same result. 316
316
And now as I was just saying, 317
317
the goal here is to create an array 318
318
containing only the dogs that are eating 319
319
an okay amount of food. 320
320
So we want a new array based on an existing array, 321
321
but only with some of the elements. 322
322
And so it is time again for using the filter method. 323
323
So docs dot filter and now this is very easy, 324
324
because we already have this condition 325
325
callback function here in this variable. 326
326
And well, we only have one dog in this condition as you see. 327
327
So that's Michael's dog, 328
328
where the recommended food is indeed pretty close 329
329
to the current food. 330
330
And so we consider that this is an okay amount of food 331
331
to eat for the dog. 332
332
Okay, almost ready here. 333
333
So I told you this is a long one. 334
334
But I hope you're having fun with this one. 335
335
This kind of seems like a real world challenge. 336
336
So like a real world task, doesn't it? 337
337
So it took quite some time and designing this challenge. 338
338
And even that was also a lot of fun. 339
339
But anyway, let's now create a copy of the dogs array, 340
340
and then sorted by the recommended food portion. 341
341
So let me actually copy this 342
342
so we don't forget what we have to do here. 343
343
So we start by creating a copy. 344
344
So dogs.copy, no dot of course, 345
345
just the name is dogs copy. 346
346
And so let's use dogs.slice for that. 347
347
And actually, we can call the sort method 348
348
then immediately on that. 349
349
So here, we are going to need a callback function as always, 350
350
which is the sorting function. 351
351
And this always has a and b as the parameters. 352
352
So we could call them something else of course here, 353
353
but I like to always call them a and b. 354
354
And actually, we don't even need this block here. 355
355
So remember that before, 356
356
when we wanted to sort an array in an ascending order, 357
357
we simply would do a minus b like this. 358
358
But this was when we had an array, simply like this. 359
359
So just with numbers, 360
360
but now we have these numbers here in objects. 361
361
And so a and b, are now the objects. 362
362
However, the values that we want to subtract 363
363
are in these objects and we can simply 364
364
get them out of there. 365
365
So we want to sort by the recommended food portion. 366
366
And so let's simply do this. 367
367
And that's actually it. 368
368
Let's call this your dog's sorted. 369
369
And then let's log that to the console here. 370
370
Dog's sorted. 371
371
Let's see and yeah, that worked. 372
372
Oh, actually we cannot see it yet. 373
373
So yeah, here is the recommended food. 374
374
So we start with the lowest one, then 191. 375
375
And you see that indeed, it is in an ascending order. 376
376
So 284 and 376. 377
377
So that worked. 378
378
And so we finished this coding challenge. 379
379
This was a lot of work. 380
380
And as I just said, also a lot of fun. 381
381
And I hope you felt the same. 382
382
And also that you felt the same for this entire section. 383
383
So this project was a great project in my opinion, 384
384
it was a lot of fun to build it together with you, 385
385
and to put it together and design it in the first place. 386
386
So I hope you're proud of what you achieved here. 387
387
And one more time I want to congratulate you 388
388
for making it this far and still sticking 389
389
with this quite long course. 390
390
Anyway with that, I see you in the next section, 391
391
where we will actually continue building 392
392
on this project by adding a couple of new things to it.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.