All language subtitles for 14. Coding Challenge

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
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
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

1 1

(Instructor)This is coding challenge number two. 2

2

And in this one, 3

3

we're gonna go back to Julia in case study about dogs. 4

4

And this time, they want to convert the ages of the dogs 5

5

to human ages, and then calculate their average age. 6

6

So your job is to create 7

7

this calc average human age function. 8

8

And this function accepts an array of dogs ages. 9

9

And it does the following four tasks, 10

10

or actually just three tasks. 11

11

So first, calculate the age of all the dogs 12

12

in human years using this formula. 13

13

So if the dog is less or equal two years old, 14

14

then the human age is simply twice the dog's age. 15

15

But if the dog is older than two years, 16

16

then the human age is calculated by this formula. 17

17

All right. 18

18

Then in the next step, exclude all the dogs 19

19

that are less than 18 human years old. 20

20

And that is essentially the same as keeping the dogs 21

21

that are at least 18 years old here okay. 22

22

Then, finally, 23

23

calculate the average human age of all the adult dogs. 24

24

And from the other challenges, you should already know how 25

25

to calculate averages. 26

26

Now, in this one, you will do it 27

27

in a different way, hopefully, 28

28

because the goal of this challenge 29

29

is of course to use the Map, Filter and Reduce methods. 30

30

Finally, then run this function 31

31

for both these test data sets. 32

32

So as I said, this is a way of training the map filter 33

33

and array methods that we just studied earlier. 34

34

And so good luck with this challenge. 35

35

Take all the time you need and they see you here 36

36

once you're ready. 37

37

So were you successful in solving this one? 38

38

I sure hope so. 39

39

And in case there was some doubt, here is my solution. 40

40

So this function takes an array of dog Ages. 41

41

And then we want to convert all of these ages to human ages. 42

42

So what we want here is to create 43

43

a new array based on the original array. 44

44

And so for that the map method is perfect. 45

45

So we will have a new array called Human Ages. 46

46

And that will be the result of calling the map method 47

47

on the ages array. 48

48

Then this callback function gets access to the current age, 49

49

so the current element in the array. 50

50

And now here what we need to return 51

51

is the result of this formula. 52

52

So this one here, 53

53

and so let's write a ternary operator here. 54

54

So if the age is less or equal two, 55

55

then what we want to return here is two times the age, 56

56

right? 57

57

Because that dog age is of course, this age variable here, 58

58

because it comes from the ages array, 59

59

which itself is an array of dog Ages. 60

60

Anyway, if the age is greater than two, 61

61

then the result will be 16 plus the age times four. 62

62

And so since this has an implicit return here, remember, 63

63

it's like writing this here, 64

64

okay? 65

65

And so it's returning either this value here or actually, 66

66

either this value, so twice the age, 67

67

or four times the age plus 16. 68

68

So that's the callback function, 69

69

and this will then produce the array that we're looking for. 70

70

Let's take a look. 71

71

So human ages, and then we just need to call the function. 72

72

And so here is the age converted to human ages. 73

73

And it does indeed look correct. 74

74

So let's assume it is correct. 75

75

Then next up we want to filter for all the dogs 76

76

that are at least 18 years old. 77

77

And so that will then exclude all the dogs 78

78

that are less than 18 years old. 79

79

So that's gonna be the adult dogs and so that is human ages. 80

80

And now we use the filter method and that makes sense right? 81

81

Because it's the filter method who allows us 82

82

to set a condition like this one. 83

83

So when the age is at least, so great or equal 18, 84

84

then put it in the new array which will be the adults array. 85

85

And so let's lock that one as well. 86

86

So actually, I want both of them here. 87

87

So human ages 88

88

and the adults. 89

89

And so indeed, these two here, 90

90

the four and the two are gone. 91

91

Alright. 92

92

And now finally, we want to calculate the average. 93

93

Okay? 94

94

And so the average is basically adding all 95

95

of these values together, 96

96

and then dividing the result by five, 97

97

so the length of the array. 98

98

So that's essentially what we already did before. 99

99

So that's gonna be adults.reduce, 100

100

then the accumulator and the current age. 101

101

And we want to return the accumulator plus the age. 102

102

And the start is gonna be at zero. 103

103

So this here will return the sum, 104

104

so all of these ages here added together. 105

105

That's now all we need to do is to divide 106

106

this by the length of this array. 107

107

And then we simply want to return 108

108

this average value that we just calculated. 109

109

So let's just see if this makes sense. 110

110

Let's actually save it here in a variable, 111

111

instead of logging into directly. 112

112

So average one, 113

113

and then also 114

114

average two. 115

115

And then just log both here to the console. 116

116

And all right, here they are. 117

117

And this looks pretty reasonable to me. 118

118

And so our solution here works. 119

119

Now, I just wanted to let that we could have done 120

120

this average calculation here in a different way. 121

121

So in a bit more complex way. 122

122

So let me show you one property of an average. 123

123

So let's say we want to calculate the average 124

124

of two and three. 125

125

So we do that 126

126

by two plus three. 127

127

So we add all of this together and then divided by two, 128

128

because we have two values. 129

129

And so that is then 2.5. 130

130

However, we can also do it in a different way. 131

131

So we can divide both of them by two immediately. 132

132

So two divided by two, 133

133

plus two divided by three, 134

134

and this will also be 2.5. 135

135

So these here are essentially the exact same thing. 136

136

Okay, and so here, we can do this differently. 137

137

So let me actually duplicate this line 138

138

So that we can keep both here. 139

139

So what I mean is that we can actually simply divide 140

140

the current age here immediately by the length of the array. 141

141

Right? 142

142

Here, this is actually three divided by two. 143

143

Okay, sorry for that mistake. 144

144

And so what we did here was basically 145

145

to divide both of the values always immediately by the two. 146

146

And so that would be the same as immediately dividing 147

147

the age here by the length of the array. 148

148

So dividing this by the length of the array. 149

149

And now remember that we can get 150

150

this length immediately here from this parameter. 151

151

Well, not really the length, but the array itself. 152

152

So that's the fourth parameter. 153

153

And so now we can say array.length, like this. 154

154

And so see what happens now here. 155

155

And indeed, the values are exactly the same. 156

156

And so this way of calculating it 157

157

is just the same as this one here, 158

158

but we can do it kind of all in one line. 159

159

And I also wanted to show you a good use case, 160

160

for using this array here. 161

161

In this case, we could of course, 162

162

also just have written here, adult.length, 163

163

but sometimes when we change 164

164

these methods together especially 165

165

then this is the only way we have 166

166

of using the current array. 167

167

And we will see a use case of that later. 168

168

Now of course, I did not expect you to write it like this, 169

169

but I just wanted to show you a good use case 170

170

for the current array parameter that we have here. 171

171

Okay, and with that, we finish the challenge, 172

172

and I see you in the next video.

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