All language subtitles for 154 Coding Challenge 2.en_US1

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian Download
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:01,184 --> 00:00:03,963 (Instructor)This is coding challenge number two. 2 00:00:05,530 --> 00:00:06,363 And in this one, 3 00:00:06,363 --> 00:00:10,730 we're gonna go back to Julia in case study about dogs. 4 00:00:10,730 --> 00:00:14,350 And this time, they want to convert the ages of the dogs 5 00:00:14,350 --> 00:00:18,243 to human ages, and then calculate their average age. 6 00:00:19,940 --> 00:00:21,370 So your job is to create 7 00:00:21,370 --> 00:00:24,720 this calc average human age function. 8 00:00:24,720 --> 00:00:28,550 And this function accepts an array of dogs ages. 9 00:00:28,550 --> 00:00:30,963 And it does the following four tasks, 10 00:00:31,890 --> 00:00:34,160 or actually just three tasks. 11 00:00:34,160 --> 00:00:37,370 So first, calculate the age of all the dogs 12 00:00:37,370 --> 00:00:40,910 in human years using this formula. 13 00:00:40,910 --> 00:00:44,720 So if the dog is less or equal two years old, 14 00:00:44,720 --> 00:00:48,850 then the human age is simply twice the dog's age. 15 00:00:48,850 --> 00:00:51,240 But if the dog is older than two years, 16 00:00:51,240 --> 00:00:55,390 then the human age is calculated by this formula. 17 00:00:55,390 --> 00:00:56,530 All right. 18 00:00:56,530 --> 00:00:59,310 Then in the next step, exclude all the dogs 19 00:00:59,310 --> 00:01:02,710 that are less than 18 human years old. 20 00:01:02,710 --> 00:01:05,840 And that is essentially the same as keeping the dogs 21 00:01:05,840 --> 00:01:10,840 that are at least 18 years old here okay. 22 00:01:11,020 --> 00:01:12,160 Then, finally, 23 00:01:12,160 --> 00:01:16,360 calculate the average human age of all the adult dogs. 24 00:01:16,360 --> 00:01:19,660 And from the other challenges, you should already know how 25 00:01:19,660 --> 00:01:22,010 to calculate averages. 26 00:01:22,010 --> 00:01:23,320 Now, in this one, you will do it 27 00:01:23,320 --> 00:01:25,480 in a different way, hopefully, 28 00:01:25,480 --> 00:01:27,280 because the goal of this challenge 29 00:01:27,280 --> 00:01:32,010 is of course to use the Map, Filter and Reduce methods. 30 00:01:32,010 --> 00:01:34,180 Finally, then run this function 31 00:01:34,180 --> 00:01:37,290 for both these test data sets. 32 00:01:37,290 --> 00:01:41,050 So as I said, this is a way of training the map filter 33 00:01:41,050 --> 00:01:44,450 and array methods that we just studied earlier. 34 00:01:44,450 --> 00:01:46,700 And so good luck with this challenge. 35 00:01:46,700 --> 00:01:49,150 line:15% Take all the time you need and they see you here 36 00:01:49,150 --> 00:01:50,163 line:15% once you're ready. 37 00:01:53,290 --> 00:01:55,883 So were you successful in solving this one? 38 00:01:57,380 --> 00:01:59,010 I sure hope so. 39 00:01:59,010 --> 00:02:03,663 And in case there was some doubt, here is my solution. 40 00:02:06,760 --> 00:02:11,140 So this function takes an array of dog Ages. 41 00:02:11,140 --> 00:02:15,740 And then we want to convert all of these ages to human ages. 42 00:02:15,740 --> 00:02:17,480 So what we want here is to create 43 00:02:17,480 --> 00:02:20,770 a new array based on the original array. 44 00:02:20,770 --> 00:02:24,023 And so for that the map method is perfect. 45 00:02:25,700 --> 00:02:30,143 So we will have a new array called Human Ages. 46 00:02:31,750 --> 00:02:35,150 And that will be the result of calling the map method 47 00:02:35,150 --> 00:02:37,000 on the ages array. 48 00:02:37,000 --> 00:02:41,580 Then this callback function gets access to the current age, 49 00:02:41,580 --> 00:02:44,130 so the current element in the array. 50 00:02:44,130 --> 00:02:46,040 And now here what we need to return 51 00:02:46,040 --> 00:02:48,713 is the result of this formula. 52 00:02:50,660 --> 00:02:52,380 So this one here, 53 00:02:52,380 --> 00:02:55,053 and so let's write a ternary operator here. 54 00:02:56,050 --> 00:02:59,733 So if the age is less or equal two, 55 00:03:01,160 --> 00:03:06,160 then what we want to return here is two times the age, 56 00:03:06,660 --> 00:03:07,493 right? 57 00:03:07,493 --> 00:03:11,510 Because that dog age is of course, this age variable here, 58 00:03:11,510 --> 00:03:14,150 because it comes from the ages array, 59 00:03:14,150 --> 00:03:17,043 which itself is an array of dog Ages. 60 00:03:19,850 --> 00:03:23,390 Anyway, if the age is greater than two, 61 00:03:23,390 --> 00:03:28,140 then the result will be 16 plus the age times four. 62 00:03:31,380 --> 00:03:35,810 And so since this has an implicit return here, remember, 63 00:03:35,810 --> 00:03:37,743 it's like writing this here, 64 00:03:38,660 --> 00:03:39,700 okay? 65 00:03:39,700 --> 00:03:44,533 And so it's returning either this value here or actually, 66 00:03:45,910 --> 00:03:48,920 either this value, so twice the age, 67 00:03:48,920 --> 00:03:51,403 or four times the age plus 16. 68 00:03:52,860 --> 00:03:54,340 So that's the callback function, 69 00:03:54,340 --> 00:03:58,390 and this will then produce the array that we're looking for. 70 00:03:58,390 --> 00:03:59,393 Let's take a look. 71 00:04:00,290 --> 00:04:04,273 So human ages, and then we just need to call the function. 72 00:04:12,200 --> 00:04:16,500 And so here is the age converted to human ages. 73 00:04:16,500 --> 00:04:18,943 And it does indeed look correct. 74 00:04:20,520 --> 00:04:22,183 So let's assume it is correct. 75 00:04:23,610 --> 00:04:27,010 Then next up we want to filter for all the dogs 76 00:04:27,010 --> 00:04:29,920 that are at least 18 years old. 77 00:04:29,920 --> 00:04:32,270 And so that will then exclude all the dogs 78 00:04:32,270 --> 00:04:34,533 that are less than 18 years old. 79 00:04:38,320 --> 00:04:43,320 So that's gonna be the adult dogs and so that is human ages. 80 00:04:44,600 --> 00:04:49,600 And now we use the filter method and that makes sense right? 81 00:04:49,610 --> 00:04:51,980 Because it's the filter method who allows us 82 00:04:51,980 --> 00:04:55,143 to set a condition like this one. 83 00:04:56,300 --> 00:05:01,300 So when the age is at least, so great or equal 18, 84 00:05:02,380 --> 00:05:06,680 then put it in the new array which will be the adults array. 85 00:05:06,680 --> 00:05:09,490 And so let's lock that one as well. 86 00:05:09,490 --> 00:05:12,870 So actually, I want both of them here. 87 00:05:12,870 --> 00:05:14,110 So human ages 88 00:05:15,830 --> 00:05:17,763 and the adults. 89 00:05:20,100 --> 00:05:22,460 And so indeed, these two here, 90 00:05:22,460 --> 00:05:24,213 the four and the two are gone. 91 00:05:25,670 --> 00:05:26,650 Alright. 92 00:05:26,650 --> 00:05:30,220 And now finally, we want to calculate the average. 93 00:05:30,220 --> 00:05:31,053 Okay? 94 00:05:31,053 --> 00:05:33,460 And so the average is basically adding all 95 00:05:33,460 --> 00:05:35,250 of these values together, 96 00:05:35,250 --> 00:05:37,810 and then dividing the result by five, 97 00:05:37,810 --> 00:05:39,493 so the length of the array. 98 00:05:41,570 --> 00:05:44,373 So that's essentially what we already did before. 99 00:05:46,820 --> 00:05:51,543 So that's gonna be adults.reduce, 100 00:05:53,050 --> 00:05:57,210 then the accumulator and the current age. 101 00:05:57,210 --> 00:06:01,813 And we want to return the accumulator plus the age. 102 00:06:02,970 --> 00:06:05,660 And the start is gonna be at zero. 103 00:06:05,660 --> 00:06:10,010 So this here will return the sum, 104 00:06:10,010 --> 00:06:13,003 so all of these ages here added together. 105 00:06:14,220 --> 00:06:16,570 That's now all we need to do is to divide 106 00:06:16,570 --> 00:06:19,633 this by the length of this array. 107 00:06:22,307 --> 00:06:25,100 And then we simply want to return 108 00:06:25,100 --> 00:06:28,553 this average value that we just calculated. 109 00:06:29,750 --> 00:06:31,713 So let's just see if this makes sense. 110 00:06:33,270 --> 00:06:35,740 Let's actually save it here in a variable, 111 00:06:35,740 --> 00:06:38,080 instead of logging into directly. 112 00:06:38,080 --> 00:06:39,283 So average one, 113 00:06:40,970 --> 00:06:42,150 and then also 114 00:06:43,740 --> 00:06:45,333 average two. 115 00:06:56,530 --> 00:07:00,363 And then just log both here to the console. 116 00:07:02,290 --> 00:07:04,870 And all right, here they are. 117 00:07:04,870 --> 00:07:07,853 And this looks pretty reasonable to me. 118 00:07:08,830 --> 00:07:11,260 And so our solution here works. 119 00:07:11,260 --> 00:07:14,290 Now, I just wanted to let that we could have done 120 00:07:14,290 --> 00:07:17,520 this average calculation here in a different way. 121 00:07:17,520 --> 00:07:20,200 So in a bit more complex way. 122 00:07:20,200 --> 00:07:22,833 So let me show you one property of an average. 123 00:07:23,670 --> 00:07:25,860 So let's say we want to calculate the average 124 00:07:25,860 --> 00:07:28,320 of two and three. 125 00:07:28,320 --> 00:07:29,600 So we do that 126 00:07:29,600 --> 00:07:32,053 by two plus three. 127 00:07:33,920 --> 00:07:37,100 So we add all of this together and then divided by two, 128 00:07:37,100 --> 00:07:39,490 because we have two values. 129 00:07:39,490 --> 00:07:42,670 And so that is then 2.5. 130 00:07:42,670 --> 00:07:45,330 However, we can also do it in a different way. 131 00:07:45,330 --> 00:07:48,313 So we can divide both of them by two immediately. 132 00:07:49,300 --> 00:07:51,970 So two divided by two, 133 00:07:51,970 --> 00:07:54,710 plus two divided by three, 134 00:07:54,710 --> 00:07:57,510 and this will also be 2.5. 135 00:07:57,510 --> 00:08:01,440 So these here are essentially the exact same thing. 136 00:08:01,440 --> 00:08:04,653 Okay, and so here, we can do this differently. 137 00:08:05,950 --> 00:08:08,460 So let me actually duplicate this line 138 00:08:09,670 --> 00:08:11,433 So that we can keep both here. 139 00:08:13,460 --> 00:08:17,950 So what I mean is that we can actually simply divide 140 00:08:17,950 --> 00:08:20,950 the current age here immediately by the length of the array. 141 00:08:22,660 --> 00:08:23,870 Right? 142 00:08:23,870 --> 00:08:26,690 Here, this is actually three divided by two. 143 00:08:26,690 --> 00:08:29,200 Okay, sorry for that mistake. 144 00:08:29,200 --> 00:08:31,110 And so what we did here was basically 145 00:08:31,110 --> 00:08:35,650 to divide both of the values always immediately by the two. 146 00:08:35,650 --> 00:08:38,630 And so that would be the same as immediately dividing 147 00:08:38,630 --> 00:08:41,700 the age here by the length of the array. 148 00:08:41,700 --> 00:08:44,460 So dividing this by the length of the array. 149 00:08:44,460 --> 00:08:46,320 And now remember that we can get 150 00:08:46,320 --> 00:08:49,493 this length immediately here from this parameter. 151 00:08:50,590 --> 00:08:54,000 Well, not really the length, but the array itself. 152 00:08:54,000 --> 00:08:56,500 So that's the fourth parameter. 153 00:08:56,500 --> 00:09:01,240 And so now we can say array.length, like this. 154 00:09:02,490 --> 00:09:04,763 And so see what happens now here. 155 00:09:06,130 --> 00:09:09,370 And indeed, the values are exactly the same. 156 00:09:09,370 --> 00:09:11,150 And so this way of calculating it 157 00:09:11,150 --> 00:09:13,240 is just the same as this one here, 158 00:09:13,240 --> 00:09:16,260 but we can do it kind of all in one line. 159 00:09:16,260 --> 00:09:18,700 And I also wanted to show you a good use case, 160 00:09:18,700 --> 00:09:20,730 for using this array here. 161 00:09:20,730 --> 00:09:22,450 In this case, we could of course, 162 00:09:22,450 --> 00:09:26,240 also just have written here, adult.length, 163 00:09:26,240 --> 00:09:27,990 but sometimes when we change 164 00:09:27,990 --> 00:09:30,150 these methods together especially 165 00:09:30,150 --> 00:09:32,880 then this is the only way we have 166 00:09:32,880 --> 00:09:35,450 of using the current array. 167 00:09:35,450 --> 00:09:38,620 And we will see a use case of that later. 168 00:09:38,620 --> 00:09:42,080 Now of course, I did not expect you to write it like this, 169 00:09:42,080 --> 00:09:44,490 but I just wanted to show you a good use case 170 00:09:44,490 --> 00:09:48,450 for the current array parameter that we have here. 171 00:09:48,450 --> 00:09:51,570 Okay, and with that, we finish the challenge, 172 00:09:51,570 --> 00:09:53,943 and I see you in the next video. 12589

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.