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
Welcome to the first coding challenge 2
2
of this section. 3
3
And as you can imagine, 4
4
this one is gonna be all about functions. 5
5
And we're back to the two gymnastic teams 6
6
from the previous section, the Dolphins and Koalas. 7
7
And now there is a new gymnastics discipline 8
8
which works in a bit different way. 9
9
So now in this new discipline, 10
10
each team competes three times 11
11
and then the average of the three scores is calculated, 12
12
so each team will get one average score. 13
13
And then a team only wins 14
14
if it has at least doubled the average score 15
15
of the other team and otherwise no team wins. 16
16
So sounds pretty simple, I hope. 17
17
And let's take a look at your tasks now. 18
18
So first create an arrow function 19
19
called calcAverage which calculates the average 20
20
of three scores. 21
21
And actually this calcAverage function 22
22
should be able to calculate the average of any three values. 23
23
So of course, the values don't have to be scores. 24
24
Any random numbers will work just the same. 25
25
So this is basically a generic function 26
26
just to calculate the average of three values. 27
27
Then use that function 28
28
to calculate the average for both teams. 29
29
And again, down here, 30
30
you have the test data that you can use. 31
31
Create a function called checkWiner 32
32
which takes the average score of each team as parameters, 33
33
and these parameters 34
34
should be called average Dolphins here and average Koalas. 35
35
And then based on these two average scores, 36
36
the function will log the winner to the console 37
37
together with the victory points. 38
38
And that of course needs to happen according to the rule 39
39
that I just explained before. 40
40
So a team only wins 41
41
if it has at least doubled the average score 42
42
of the other team, 43
43
and otherwise no team wins. 44
44
So you need to check for that condition 45
45
in order to print which team wins, 46
46
and in some cases, no team might win at all. 47
47
So you need to account for that as well. 48
48
So here in point three you write to checkWiner function 49
49
and then a number four you will use. 50
50
So basically you will call the checkWinner function 51
51
in order to determine the winner 52
52
for both to test data one and test data two. 53
53
I have some hints again, 54
54
but if you want to try it on your own without the hints 55
55
then just past the video right now. 56
56
But if you need to hints 57
57
then well, here they go. 58
58
So this one is probably pretty obvious 59
59
which is to calculate the average of three values, 60
60
add them altogether and divide by three. 61
61
But the second one here is a little bit more important 62
62
and maybe more difficult for you to figure out on your own. 63
63
So to check if number A is at least double number B, 64
64
what you need to check is if A 65
65
is greater or equal two times B. 66
66
And so this is what you will use 67
67
with the teams average scores. 68
68
So it's important to keep this one in mind 69
69
because of this rule, 70
70
that the team only wins when it has doubled average score 71
71
of the other team. 72
72
Now, please make really sure 73
73
that you understood everything I showed you about functions 74
74
before you attempt this challenge. 75
75
Because this one is not so straight forward, 76
76
and so I don't want you to get frustrated. 77
77
So again, please really make sure you understand functions 78
78
and only then attempt to do this challenge. 79
79
But if you feel that you're ready, 80
80
then just pause the video now 81
81
and good luck with this challenge. 82
82
Let me show you my solution to this challenge, 83
83
which I hope of course you were able to do on your own. 84
84
So I'm gonna start 85
85
with the calcAverage function as a number one, 86
86
and remember that this should be an arrow function 87
87
and it will receive three values 88
88
so that it can calculate the average 89
89
of these three numbers. 90
90
And here I'm actually just gonna call 91
91
the three numbers, A, B and C, 92
92
because again, this is supposed to be just 93
93
a generic function which can calculate averages 94
94
of any three numbers, 95
95
it doesn't have to be any scores. 96
96
And so A, B, C are just generic variable names. 97
97
But of course it's totally okay 98
98
if you used more specific variable names. 99
99
So adding all the three numbers together 100
100
and then dividing by three. 101
101
So this one is nothing new, 102
102
this was just to basically test your knowledge 103
103
of the syntax of the arrow function. 104
104
So what's important to notice here 105
105
is that we need to parenthesis here 106
106
because we have multiple parameters, 107
107
but then here, we only really have this line of code. 108
108
And so therefore we don't need to even write the return. 109
109
This value gets automatically returned from the function. 110
110
And now let's just test it out 111
111
before we move any further. 112
112
So calcAverage and then let's just use some random numbers 113
113
because here we know that this is supposed to be four. 114
114
So we have three, four, five. 115
115
And so the middle one here 116
116
is the average between these three. 117
117
And indeed it is, and so we know that our function works. 118
118
And so we can move on to point number two, 119
119
which is to use the function 120
120
to calculate the average for both teams. 121
121
So let's say score Dolphins 122
122
is equal to calcAverage 123
123
and now we pass into specific arguments 124
124
for the three parameters, A, B and C. 125
125
And so for test data one, which is this, 126
126
so let me actually write that here, test one. 127
127
So for the Dolphins, we pass into values 44, 23 and 71. 128
128
Then for the Koalas, it works the same way. 129
129
So score Koalas equals calcAverage. 130
130
And now we have 65, 54 and 49. 131
131
Nice reusable function right here. 132
132
And so having this function, 133
133
we don't need to calculate it here manually 134
134
every single time. 135
135
So that's really great. 136
136
We can really appreciate the fact that again, 137
137
a function is really like a machine in some sense. 138
138
So it doesn't care where the inputs come from 139
139
and where they go. 140
140
All it does is to calculate the average of three numbers, 141
141
no matter if it's scores or heights of people 142
142
or money or whatever it is. 143
143
So this function really is kind of isolated 144
144
from the rest of the code here. 145
145
It's a standalone function that we can then use 146
146
for our own purposes. 147
147
And that might sound pretty obvious, 148
148
but it's actually important that you really internalize 149
149
this aspect of functions. 150
150
Only then can you really start 151
151
to see the potential for functions everywhere in your code. 152
152
Anyway, with that being said, 153
153
let's now log these values to the console 154
154
'cause I always like to see what I'm doing. 155
155
So this part is of course not mandatory, 156
156
but it's also nice to be able to know the scores 157
157
so that we know if the logic 158
158
that we're gonna implement next is working correctly. 159
159
So we see that in this case, 160
160
the Koalas have a score that is higher 161
161
than one of the Dolphins but it's not double. 162
162
And so we already know that in this case 163
163
no team should be able to win, 164
164
but let's not actually implement that functionality. 165
165
So that's the function checkWinner. 166
166
And so it takes the average Dolphins 167
167
and average Koalas parameters 168
168
and then it locks the winner to the console. 169
169
So let's do that. 170
170
And what's more, it's no problem 171
171
if you did this differently. 172
172
If you made it work then it's great. 173
173
So average Dolphins and average Koalas. 174
174
And now all they need to do is to check 175
175
whether the Dolphins won or the Koalas won 176
176
or if no one won basically. 177
177
So when do the Dolphins win? 178
178
Well, they win 179
179
if they have doubled the points of the Koalas, 180
180
that's the condition. 181
181
So again, a team only wins 182
182
if it has at least doubled the average score 183
183
of the other team. 184
184
And so that's why I told you here in this hint 185
185
that this is how we check for that. 186
186
So A has double the amount of points of B 187
187
if A is greater than two times B. 188
188
So let's do that. 189
189
So average Dolphins greater 190
190
or equal to two times the average Koalas. 191
191
And in this case, we look to the console 192
192
Dolphins win the trophy, 193
193
let's use an emoji here again 194
194
but you don't need of course to do that. 195
195
So it's already here, 196
196
and now it also told us to put the points. 197
197
For example, 30 versus 13, 198
198
so both points with the versus in between. 199
199
So that's average Dolphins 200
200
versus average Koalas. 201
201
So you'll see that here 202
202
we are also practicing all the other skills 203
203
that we already acquired before. 204
204
Especially in the previous section. 205
205
Then now here we need to just need to do the opposite. 206
206
So else if, 207
207
and now if the Koalas half at least. 208
208
So that's basically what this one here means. 209
209
So greater equal is basically at least. 210
210
So if average Koalas 211
211
have at least double the points of the Dolphins, 212
212
then the Koalas win. 213
213
So that's just grab this one from here. 214
214
Koalas and tier it's average Koalas, 215
215
and then average Dolphins, average, yes. 216
216
And now in any other case, 217
217
we say that no one wins. 218
218
So I think that's also said in the rules. 219
219
Yeah, that's a number of five. 220
220
I think I even forgot to tell you this one 221
221
but hopefully you read it. 222
222
So you should ignore draws in this exercise this time. 223
223
So if none of these is true, 224
224
we simply say that no team wins. 225
225
No team wins. 226
226
And now all we need to do is to call this function here. 227
227
So checkWinner, and we will do that 228
228
with score Dolphins 229
229
and score Koalas. 230
230
Let's recap, we calculated the score of the Dolphins up here 231
231
and of the Koalas as well. 232
232
And now we call the checkWiner function here 233
233
and we use as arguments 234
234
the scores that we calculated before 235
235
and then score Dolphins will become average Dolphins 236
236
here in this function, 237
237
because that's the name of the parameter. 238
238
And I see that I misspelled this, 239
239
and so now I can use that command D 240
240
that I talked to you about before, 241
241
so I can select all of them. 242
242
So command D, command D,command D, command D 243
243
and now I can use all of these cursors here 244
244
at the same time. 245
245
So Dolphins you see, 246
246
I inserted the P in all of the variable names everywhere. 247
247
And so now this parameter name here gets used 248
248
for all these calculations. 249
249
And again, originally this value comes basically from here, 250
250
from calculating the average of these three scores. 251
251
So this average gets stored here 252
252
then we use it here 253
253
and then it gets basically transferred to average Dolphins 254
254
and is then used everywhere in this function. 255
255
So let's now test. 256
256
And as we expected, no team wins 257
257
because this team here 258
258
even though it has more than the Dolphins, it's not double. 259
259
And so therefore they do not win. 260
260
Now of course, what is also important to mention here 261
261
is that this checkWiner function here 262
262
is also completely independent of these score values 263
263
that we calculated earlier. 264
264
So here we can simply call this function 265
265
with completely different values. 266
266
So the function doesn't care 267
267
where do these values come from. 268
268
So where average Dolphins and average Koalas come from. 269
269
It's a standalone function 270
270
and it doesn't care at all, where it is values came from. 271
271
We can just plug in some random numbers here 272
272
and then call it and let's use a smaller number here. 273
273
So just to that, the average Dolphin here 274
274
is actually more than double of the average Koalas. 275
275
So if you run this now, 276
276
then we see that the Dolphins win with 576 versus 111. 277
277
And so that's exactly the numbers 278
278
that we plugged in in here. 279
279
So once more, it's important to keep in mind 280
280
that these functions are all independent from another. 281
281
We just happen to call checkWiner here 282
282
with these values that we calculated before, 283
283
but it's not mandatory to do that, 284
284
any other values will work just as well. 285
285
And so we proved that here. 286
286
Anyway, now let's use test data two, 287
287
and actually what I'm gonna do 288
288
is to reassign these two scores here. 289
289
So we calculate them first, 290
290
then we call checkWinner 291
291
and then I'm going to recalculate them 292
292
and call checkWinner again. 293
293
What I mean by saying that 294
294
is that these need to be let variables 295
295
so that we can reassign them, remember that? 296
296
Const values cannot be mutated, 297
297
they cannot be changed, 298
298
and so they need to be let. 299
299
And then here, of course, we need to get rid of the let, 300
300
because we do not want to create new variables, 301
301
we just want to overwrite. 302
302
So we want to reassign 303
303
the values that we had earlier. 304
304
So this time the values 305
305
are 85, 54 and 41. 306
306
85, 54 and 41. 307
307
And I hope that's correct. 308
308
And then for the Koalas, 309
309
we have 23, 34 and 27. 310
310
So that's 23, 34 and 27. 311
311
Let's lock these actually also, 312
312
and then simply call checkWinner again, 313
313
and now with the new values 314
314
of score Dolphins and score Koalas that we just reassigned. 315
315
So let's see. 316
316
And now the Dolphins seven average score of 60 317
317
and to Koalas of 28. 318
318
And so now the Dolphins win 319
319
with a 60 versus 28. 320
320
So that's a close call. 321
321
So if the Koalas had three more points 322
322
then no one would win, 323
323
but the Dolphins managed to do it. 324
324
Great, I hope that this all made sense to you, 325
325
and I know it is probably still 326
326
a bit confusing at this point. 327
327
And if you did some things differently 328
328
well then once more data's completely okay. 329
329
It just means you're developing 330
330
your own coding style over time. 331
331
For example, here with the average scores, 332
332
of course you could have created new variables down here 333
333
for the test data too. 334
334
So that's just one of many things 335
335
that you could have done differently 336
336
which are perfectly fine. 337
337
As long as your results are the same, you're good to go. 338
338
So take some time and review this lecture. 339
339
Remember that you can check out my final code 340
340
also in the final folder and the GitHub repo, 341
341
and then you're ready to move on to our next big topic 342
342
in the section, 343
343
which is gonna be arrays. 344
344
So I hope to see you there soon.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.