1
00:00:00,300 --> 00:00:05,300
So let me show you my solution to this challenge

2
00:00:05,430 --> 00:00:09,330
which I hope of course, you were able to do on your own.

3
00:00:09,330 --> 00:00:12,540
So I'm gonna start with the calc average function,

4
00:00:12,540 --> 00:00:14,163
as in number one.

5
00:00:15,150 --> 00:00:17,880
And remember that this should be an arrow function,

6
00:00:17,880 --> 00:00:20,250
and it will receive three values,

7
00:00:20,250 --> 00:00:24,600
so that it can calculate the average of these three numbers.

8
00:00:24,600 --> 00:00:27,240
And here, I'm actually just gonna call the three numbers,

9
00:00:27,240 --> 00:00:32,189
A, B, and C, because again,

10
00:00:32,189 --> 00:00:35,280
this is supposed to be just a generic function

11
00:00:35,280 --> 00:00:38,850
which can calculate averages of any three numbers.

12
00:00:38,850 --> 00:00:40,830
It doesn't have to be any scores.

13
00:00:40,830 --> 00:00:44,670
And so, A, B, C, are just generic, variable names,

14
00:00:44,670 --> 00:00:47,070
but of course, it's totally okay

15
00:00:47,070 --> 00:00:50,073
if you used more specific variable names.

16
00:00:51,840 --> 00:00:54,180
So adding all the three numbers together,

17
00:00:54,180 --> 00:00:56,340
and then dividing by three.

18
00:00:56,340 --> 00:00:57,810
So this one is nothing new.

19
00:00:57,810 --> 00:01:00,540
This was just to basically test your knowledge

20
00:01:00,540 --> 00:01:03,360
of the syntax of the arrow function.

21
00:01:03,360 --> 00:01:05,099
So what's important to notice here

22
00:01:05,099 --> 00:01:06,990
is that we need the parenthesis here

23
00:01:06,990 --> 00:01:09,600
because we have multiple parameters,

24
00:01:09,600 --> 00:01:12,840
but then here, we only really have this line of code.

25
00:01:12,840 --> 00:01:16,770
And so therefore, we don't need to even write the return.

26
00:01:16,770 --> 00:01:20,700
This value gets automatically returned from the function.

27
00:01:20,700 --> 00:01:24,810
And now, let's just test it out before we move any further.

28
00:01:24,810 --> 00:01:29,810
So calc average and then let's just use some random numbers

29
00:01:30,390 --> 00:01:32,880
because here we know that

30
00:01:32,880 --> 00:01:36,630
this is supposed to be four, right?

31
00:01:36,630 --> 00:01:38,100
So we have 3, 4, 5.

32
00:01:38,100 --> 00:01:41,280
And so the middle one here is the average

33
00:01:41,280 --> 00:01:43,023
between these three,

34
00:01:44,610 --> 00:01:46,230
and indeed it is.

35
00:01:46,230 --> 00:01:48,630
And so, we know that our function works.

36
00:01:48,630 --> 00:01:51,570
And so, we can move on to point number two

37
00:01:51,570 --> 00:01:53,760
which is to use the function to calculate

38
00:01:53,760 --> 00:01:55,923
the average for both teams.

39
00:01:57,420 --> 00:02:01,320
So let's say,

40
00:02:01,320 --> 00:02:06,320
score dolphins is equal to calc average.

41
00:02:08,160 --> 00:02:10,350
And now we passed in the specific arguments

42
00:02:10,350 --> 00:02:13,950
for the three parameters, A, B, and C.

43
00:02:13,950 --> 00:02:16,890
And so, for test data one, which is this,

44
00:02:16,890 --> 00:02:20,370
so let me actually write that here, test one.

45
00:02:20,370 --> 00:02:25,370
So for the dolphins, we pass in the values 44, 23, and 71.

46
00:02:28,560 --> 00:02:32,220
Then for the koalas, it works the same way.

47
00:02:32,220 --> 00:02:36,540
So score, koalas equals calc average,

48
00:02:36,540 --> 00:02:40,593
and now, we have 65, 54,

49
00:02:41,880 --> 00:02:43,293
and 49.

50
00:02:44,370 --> 00:02:47,613
So nice reusable function right here.

51
00:02:48,660 --> 00:02:50,970
And so, having this function, we don't need

52
00:02:50,970 --> 00:02:54,450
to calculate it here manually every single time.

53
00:02:54,450 --> 00:02:56,103
So that's really great.

54
00:02:56,940 --> 00:03:00,870
So we can really appreciate the fact that again,

55
00:03:00,870 --> 00:03:04,890
a function is really like a machine in some sense.

56
00:03:04,890 --> 00:03:07,530
So it doesn't care where the inputs come from

57
00:03:07,530 --> 00:03:09,360
and where they go.

58
00:03:09,360 --> 00:03:12,930
All it does is to calculate the average of three numbers,

59
00:03:12,930 --> 00:03:16,230
no matter if it scores or heights of people,

60
00:03:16,230 --> 00:03:18,900
or money, or whatever it is.

61
00:03:18,900 --> 00:03:21,540
So this function really is kind of isolated

62
00:03:21,540 --> 00:03:23,730
from the rest of the code here.

63
00:03:23,730 --> 00:03:25,320
It's a standalone function

64
00:03:25,320 --> 00:03:29,010
that we can then use for our own purposes.

65
00:03:29,010 --> 00:03:31,230
And that might sound pretty obvious,

66
00:03:31,230 --> 00:03:32,580
but it's actually important

67
00:03:32,580 --> 00:03:36,720
that you really internalize this aspect of functions.

68
00:03:36,720 --> 00:03:39,870
Only then can you really start to see the potential

69
00:03:39,870 --> 00:03:43,380
for functions everywhere in your coat.

70
00:03:43,380 --> 00:03:48,380
Anyway, with that being said, let's now log these values

71
00:03:48,600 --> 00:03:53,600
to the console 'cause I always like to see what I'm doing.

72
00:03:54,000 --> 00:03:56,373
So this part is of course not mandatory,

73
00:03:57,300 --> 00:04:00,120
but it's also nice to be able to know these scores,

74
00:04:00,120 --> 00:04:02,280
so that we know if the logic that we're

75
00:04:02,280 --> 00:04:05,163
gonna implement next is working correctly.

76
00:04:06,030 --> 00:04:08,730
So we see that in this case,

77
00:04:08,730 --> 00:04:11,490
the koalas have a score that is higher

78
00:04:11,490 --> 00:04:14,940
than one of the dolphins, but it's not double.

79
00:04:14,940 --> 00:04:17,550
And so, we already know that in this case,

80
00:04:17,550 --> 00:04:20,459
no team should be able to win,

81
00:04:20,459 --> 00:04:23,433
but let's now actually implement that functionality.

82
00:04:24,420 --> 00:04:27,270
So that's the function check winner.

83
00:04:27,270 --> 00:04:29,520
And so, it takes the average dolphins

84
00:04:29,520 --> 00:04:33,417
and average koalas parameters, okay?

85
00:04:33,417 --> 00:04:36,450
And then it locks the winner to the console.

86
00:04:36,450 --> 00:04:38,253
So let's do that.

87
00:04:39,510 --> 00:04:42,993
And once more, it's no problem if you did this differently.

88
00:04:45,120 --> 00:04:47,823
If you made it work, then it's great.

89
00:04:49,350 --> 00:04:54,350
So average dolphins and average koalas.

90
00:04:57,660 --> 00:04:59,850
And now, all we need to do is to check

91
00:04:59,850 --> 00:05:01,410
whether the dolphins won

92
00:05:01,410 --> 00:05:06,090
or the koalas won, or if no one won basically.

93
00:05:06,090 --> 00:05:08,880
So when do the dolphins win?

94
00:05:08,880 --> 00:05:11,750
Well, they win if they have doubled the points

95
00:05:11,750 --> 00:05:13,680
of the koalas right?

96
00:05:13,680 --> 00:05:14,730
That's the condition.

97
00:05:16,260 --> 00:05:18,960
So again, a team only wins if it has

98
00:05:18,960 --> 00:05:22,410
at least double the average score of the other team.

99
00:05:22,410 --> 00:05:25,980
And so, that's why I told you here in this hint

100
00:05:25,980 --> 00:05:27,783
that this is how we check for that.

101
00:05:28,890 --> 00:05:32,760
So A has doubled the amount of point of B

102
00:05:32,760 --> 00:05:35,583
if A is greater than two times B.

103
00:05:37,140 --> 00:05:39,123
So let's do that.

104
00:05:40,920 --> 00:05:45,000
So average dolphins greater or equal

105
00:05:45,000 --> 00:05:50,000
to two times the average koalas.

106
00:05:50,820 --> 00:05:53,883
And in this case, we log to the console.

107
00:05:57,660 --> 00:06:01,110
Dolphins win the trophy.

108
00:06:01,110 --> 00:06:02,883
So let's use an emoji here, again,

109
00:06:03,720 --> 00:06:06,240
but you don't need of course to do that.

110
00:06:06,240 --> 00:06:07,413
So it's already here.

111
00:06:08,640 --> 00:06:12,390
And now, it also told us to put the points.

112
00:06:12,390 --> 00:06:17,353
So for example, 30 versus 13.

113
00:06:19,260 --> 00:06:22,410
So both points with the versus in between.

114
00:06:22,410 --> 00:06:27,410
So that's average dolphins versus average koalas.

115
00:06:32,820 --> 00:06:35,760
So you see that here, we are also practicing all

116
00:06:35,760 --> 00:06:39,360
the other skills that we already acquired before,

117
00:06:39,360 --> 00:06:41,973
especially in the previous section, right?

118
00:06:43,620 --> 00:06:46,980
Okay. And now here, we gonna just need to do the opposite.

119
00:06:46,980 --> 00:06:51,540
So else if and now

120
00:06:51,540 --> 00:06:55,320
if the koalas have at least,

121
00:06:55,320 --> 00:06:58,380
so that's basically what this one here means.

122
00:06:58,380 --> 00:07:01,770
So greater equal is basically at least.

123
00:07:01,770 --> 00:07:05,260
So if average koalas have at least double the points

124
00:07:06,300 --> 00:07:11,300
of the dolphins, then the koalas win.

125
00:07:11,700 --> 00:07:14,433
So let's just grab this one from here,

126
00:07:15,780 --> 00:07:16,713
koalas,

127
00:07:19,650 --> 00:07:20,640
average koalas,

128
00:07:20,640 --> 00:07:24,003
and then average dolphins,

129
00:07:25,500 --> 00:07:26,883
average, yes.

130
00:07:27,840 --> 00:07:31,410
All right, and now, in any other case,

131
00:07:31,410 --> 00:07:33,990
we say that no one wins.

132
00:07:33,990 --> 00:07:36,273
So I think that's also said in the rules.

133
00:07:37,470 --> 00:07:39,360
Yeah, that's number five.

134
00:07:39,360 --> 00:07:42,480
I think I even forgot to tell you this one,

135
00:07:42,480 --> 00:07:44,250
but hopefully you read it.

136
00:07:44,250 --> 00:07:49,250
So you should ignore draws in this exercise this time.

137
00:07:50,220 --> 00:07:51,810
So if none of these is true,

138
00:07:51,810 --> 00:07:53,763
we simply say that no team wins.

139
00:07:57,030 --> 00:08:00,153
No team wins.

140
00:08:01,770 --> 00:08:02,760
Okay.

141
00:08:02,760 --> 00:08:06,663
And now, all we need to do is to call this function here.

142
00:08:07,560 --> 00:08:11,520
So check winner, and we will do that

143
00:08:11,520 --> 00:08:13,570
with score dolphins

144
00:08:14,610 --> 00:08:18,063
and score koalas.

145
00:08:19,140 --> 00:08:21,930
Okay? So let's recap.

146
00:08:21,930 --> 00:08:23,160
We calculated the score

147
00:08:23,160 --> 00:08:27,090
of the dolphins up here and of the koalas as well.

148
00:08:27,090 --> 00:08:32,090
And now, we call the check winner function here, okay?

149
00:08:32,100 --> 00:08:33,600
And we use as arguments,

150
00:08:33,600 --> 00:08:36,150
the scores that we calculated before.

151
00:08:36,150 --> 00:08:39,669
And then, score dolphins will become average dolphins

152
00:08:41,159 --> 00:08:42,330
here in this function

153
00:08:42,330 --> 00:08:44,760
because that's the name of the parameter.

154
00:08:44,760 --> 00:08:46,950
And I see that I misspelled this,

155
00:08:46,950 --> 00:08:50,310
and so, now I can use that command D that I talked

156
00:08:50,310 --> 00:08:53,370
to you about before, so I can select all of them.

157
00:08:53,370 --> 00:08:54,750
So command D, command D,

158
00:08:54,750 --> 00:08:58,230
command D, command D, and now I can use all

159
00:08:58,230 --> 00:09:00,660
of these cursors here at the same time.

160
00:09:00,660 --> 00:09:01,713
So dolphins.

161
00:09:03,810 --> 00:09:06,390
You see? I inserted the P in all

162
00:09:06,390 --> 00:09:08,943
of the variable names everywhere.

163
00:09:10,980 --> 00:09:12,330
Now, all right?

164
00:09:12,330 --> 00:09:15,630
And so, now this parameter name here gets used

165
00:09:15,630 --> 00:09:17,940
for all these calculations.

166
00:09:17,940 --> 00:09:22,170
And again, originally, this value comes basically from here,

167
00:09:22,170 --> 00:09:26,040
from calculating the average of these three scores.

168
00:09:26,040 --> 00:09:30,180
So this average gets stored here, then we use it here,

169
00:09:30,180 --> 00:09:34,020
and then it gets basically transferred to average dolphins,

170
00:09:34,020 --> 00:09:36,423
and is then used everywhere in this function.

171
00:09:37,680 --> 00:09:40,080
Okay? So let's now test.

172
00:09:40,080 --> 00:09:44,580
And as we expected, no team wins because this team here,

173
00:09:44,580 --> 00:09:48,510
even though it has more than the dolphins, it's not double.

174
00:09:48,510 --> 00:09:50,493
And so therefore, they do not win.

175
00:09:51,360 --> 00:09:54,660
Now of course, what is also important to mention here

176
00:09:54,660 --> 00:09:57,960
is that this check winner function here

177
00:09:57,960 --> 00:10:00,390
is also completely independent

178
00:10:00,390 --> 00:10:04,410
of these score values that we calculated earlier.

179
00:10:04,410 --> 00:10:07,440
So here, we can simply call this function

180
00:10:07,440 --> 00:10:09,510
with completely different values.

181
00:10:09,510 --> 00:10:12,810
So the function doesn't care where these values come from,

182
00:10:12,810 --> 00:10:16,560
so where average dolphins and average koalas come from.

183
00:10:16,560 --> 00:10:18,240
It's a standalone function,

184
00:10:18,240 --> 00:10:21,360
and it doesn't care at all where these values came from.

185
00:10:21,360 --> 00:10:23,560
We can just plug in some random numbers here

186
00:10:25,140 --> 00:10:29,040
and then call it, and let's use a smaller number here.

187
00:10:29,040 --> 00:10:32,310
Okay, so just so that the average dolphin here

188
00:10:32,310 --> 00:10:35,223
is actually more than double of the average koalas.

189
00:10:36,120 --> 00:10:38,490
So if you run this now,

190
00:10:38,490 --> 00:10:43,490
then we see that the dolphins win with 576 versus 111.

191
00:10:44,760 --> 00:10:46,530
And so, that's exactly the numbers

192
00:10:46,530 --> 00:10:50,400
that we plugged in in here, okay?

193
00:10:50,400 --> 00:10:53,190
So once more, it's important to keep in mind

194
00:10:53,190 --> 00:10:56,310
that these functions are all independent from another.

195
00:10:56,310 --> 00:10:59,010
We just happen to call check winner here

196
00:10:59,010 --> 00:11:01,500
with these values that we calculated before,

197
00:11:01,500 --> 00:11:03,600
but it's not mandatory to do that.

198
00:11:03,600 --> 00:11:06,810
Any other values will work just as well.

199
00:11:06,810 --> 00:11:08,193
And so, we prove that here.

200
00:11:09,210 --> 00:11:13,020
Anyway, now, let's use test data 2.

201
00:11:13,020 --> 00:11:14,460
And actually what I'm gonna do is

202
00:11:14,460 --> 00:11:17,850
to reassign these two scores here.

203
00:11:17,850 --> 00:11:21,840
So we calculate them first, then we call check winner,

204
00:11:21,840 --> 00:11:24,240
and then I'm going to recalculate them

205
00:11:24,240 --> 00:11:26,670
and call check winner again.

206
00:11:26,670 --> 00:11:30,240
So what I mean by saying that is that these need

207
00:11:30,240 --> 00:11:34,470
to be let variables, so that we can reassign them.

208
00:11:34,470 --> 00:11:39,060
Remember that cons values cannot be mutated.

209
00:11:39,060 --> 00:11:43,710
They cannot be changed, and so they need to be let.

210
00:11:43,710 --> 00:11:46,740
And then here, of course, we need to get rid of

211
00:11:46,740 --> 00:11:50,550
that let because we do not want to create new variables.

212
00:11:50,550 --> 00:11:52,260
We just want to overwrite.

213
00:11:52,260 --> 00:11:55,893
So we want to reassign the values that we had earlier.

214
00:11:57,510 --> 00:12:00,863
So this time, the values are 85, 54, and 41.

215
00:12:07,710 --> 00:12:12,710
85, 54, and 41,

216
00:12:12,840 --> 00:12:14,880
and I hope that's correct,

217
00:12:14,880 --> 00:12:17,553
and then 40 koalas.

218
00:12:18,840 --> 00:12:22,413
We have 23, 34, and 27.

219
00:12:24,240 --> 00:12:29,053
So that's 23, 34, and 27.

220
00:12:31,650 --> 00:12:33,753
Let's lock these actually also.

221
00:12:34,710 --> 00:12:39,420
And then simply call check winner again.

222
00:12:39,420 --> 00:12:41,010
And now, with the new values

223
00:12:41,010 --> 00:12:43,590
of score dolphins and score koalas,

224
00:12:43,590 --> 00:12:46,833
they'll be just reassigned, okay?

225
00:12:47,820 --> 00:12:50,013
So let's see.

226
00:12:51,060 --> 00:12:53,070
And now, the dolphins have an average score

227
00:12:53,070 --> 00:12:55,800
of 60 and the koalas of 28.

228
00:12:55,800 --> 00:13:00,800
And so now, the dolphins win with 60 versus 28,

229
00:13:01,350 --> 00:13:03,450
so that's a close call.

230
00:13:03,450 --> 00:13:05,670
So if the koalas had three more points,

231
00:13:05,670 --> 00:13:07,230
then no one would win,

232
00:13:07,230 --> 00:13:09,633
but the dolphins managed to do it.

233
00:13:10,950 --> 00:13:15,950
Great. So I hope that this all made sense to you,

234
00:13:16,530 --> 00:13:18,720
and I know it is probably still

235
00:13:18,720 --> 00:13:21,390
a bit confusing at this point.

236
00:13:21,390 --> 00:13:23,520
And if you did some things differently,

237
00:13:23,520 --> 00:13:26,700
well then once more, that is completely okay.

238
00:13:26,700 --> 00:13:28,080
It just means you're developing

239
00:13:28,080 --> 00:13:31,290
your own coding style over time.

240
00:13:31,290 --> 00:13:34,110
For example, here with the average scores,

241
00:13:34,110 --> 00:13:37,440
of course, you could have created new variables down here

242
00:13:37,440 --> 00:13:39,510
for the test data 2.

243
00:13:39,510 --> 00:13:41,100
So that's just one of many things

244
00:13:41,100 --> 00:13:42,930
that you could have done differently

245
00:13:42,930 --> 00:13:44,400
which are perfectly fine.

246
00:13:44,400 --> 00:13:48,300
As long as your results are the same, you are good to go,

247
00:13:48,300 --> 00:13:51,390
so take some time and review this lecture.

248
00:13:51,390 --> 00:13:54,600
Remember that you can check out my final code also

249
00:13:54,600 --> 00:13:58,560
in the final folder, in the GitHub repo,

250
00:13:58,560 --> 00:14:02,790
and then you're ready to move on to our next big topic

251
00:14:02,790 --> 00:14:05,820
in the section, which is gonna be a race.

252
00:14:05,820 --> 00:14:07,833
So I hope to see you there soon.

