1
00:00:09,220 --> 00:00:16,120
Hey, everyone, let's get started with exercise number two, so the Jason below represents a list of

2
00:00:16,120 --> 00:00:16,810
courses.

3
00:00:17,170 --> 00:00:25,570
Each course has a title and an array of reviews create a function to pass the Jason, add another property

4
00:00:25,570 --> 00:00:32,670
for the average rating of each course, then return a new JSON with the updated object.

5
00:00:33,190 --> 00:00:36,520
So we also have some additional rules down here.

6
00:00:36,910 --> 00:00:40,980
But let's get started with the first part of the exercise.

7
00:00:41,320 --> 00:00:43,300
So let's just copy this.

8
00:00:43,840 --> 00:00:49,770
Let's pretend we just received this data from an external source like an API.

9
00:00:50,020 --> 00:00:53,740
So going back to Visual Studio Code, let's base this.

10
00:00:54,430 --> 00:01:00,100
We have some RAD underlines here, which indicates errors.

11
00:01:00,670 --> 00:01:03,340
But this is just because we broke lines.

12
00:01:04,030 --> 00:01:09,340
We can just move everything into a single line like this.

13
00:01:11,280 --> 00:01:13,050
And now we are good.

14
00:01:15,010 --> 00:01:22,630
All right, so let's create our function, so I'm just going to create a new function here called maybe

15
00:01:22,630 --> 00:01:23,500
average.

16
00:01:24,630 --> 00:01:26,460
Reviews or something like this?

17
00:01:29,970 --> 00:01:36,840
Now we need to pass an argument which is going to be our Jason, so for this, since we are simulating

18
00:01:36,990 --> 00:01:40,680
a request to an API, I'm just going to call it data.

19
00:01:41,850 --> 00:01:48,300
So the data that we are going to pass as an argument is just going to be this, Jason, that we have

20
00:01:48,300 --> 00:01:49,060
right here.

21
00:01:49,350 --> 00:01:54,920
So the first thing we're going to do when we start dysfunction is creating a variable.

22
00:01:55,680 --> 00:01:57,540
I'm going to call it causes.

23
00:01:57,900 --> 00:02:04,950
And we shouldn't worry about any conflict between these two because this variable here is inside the

24
00:02:04,950 --> 00:02:07,530
local scope of this function.

25
00:02:07,680 --> 00:02:10,340
So this is completely fine.

26
00:02:10,800 --> 00:02:14,130
So, of course, this is going to be Jason.

27
00:02:15,160 --> 00:02:16,210
That pass.

28
00:02:18,640 --> 00:02:21,520
And then we are going to pass the data.

29
00:02:22,060 --> 00:02:27,910
So if we do this, we should have our data as a JavaScript object.

30
00:02:29,040 --> 00:02:36,650
So let's do a consult that clock just to see if everything's working, so let's send the courses, which

31
00:02:36,650 --> 00:02:42,040
again, it's not going to be this one, but this one inside the local scope of the function.

32
00:02:42,800 --> 00:02:48,560
And we also need to execute the function on page load.

33
00:02:49,720 --> 00:02:56,920
So right here we are just creating this function, we still need to execute it, so let's do it right

34
00:02:56,920 --> 00:02:57,210
here.

35
00:02:57,610 --> 00:03:06,610
And as an argument for this function, we are just going to pass the courses variable, which is the

36
00:03:06,610 --> 00:03:08,160
one in the global scope.

37
00:03:08,170 --> 00:03:12,830
So this one, it is going to be passed as an argument of this function.

38
00:03:12,850 --> 00:03:16,300
So everything should work if we do like this.

39
00:03:16,930 --> 00:03:17,800
Let's save this.

40
00:03:18,520 --> 00:03:20,220
Let's go back to our page.

41
00:03:20,230 --> 00:03:22,210
I'm going to refresh the page.

42
00:03:25,560 --> 00:03:31,440
If I go to the inspector, let's go to the consul, and now we are seeing an array.

43
00:03:32,970 --> 00:03:40,000
So every element of this array is a course which has title and reviews.

44
00:03:40,680 --> 00:03:43,320
So, again, title reviews.

45
00:03:44,430 --> 00:03:47,520
Pretty cool, so we're getting somewhere here.

46
00:03:47,760 --> 00:03:55,950
All right, so now we need to add the average rating for each of the courses, but we have the second

47
00:03:55,950 --> 00:04:03,670
part of the exercise, which is using the tri catch intro to comply with the following rules.

48
00:04:03,930 --> 00:04:11,430
So if the course doesn't have any reviews, so instead of showing zero, we need to show no reviews.

49
00:04:11,730 --> 00:04:19,320
So then we are not going to show a number, but we are going to show a string that says no reviews if

50
00:04:19,320 --> 00:04:22,020
the course has less than five reviews.

51
00:04:22,290 --> 00:04:28,870
So from one to five reviews, we should show not enough reviews yet.

52
00:04:28,890 --> 00:04:32,550
So we are still not going to show the average.

53
00:04:32,850 --> 00:04:41,570
Otherwise the property should receive the average rating of the course rounded to one decimal place.

54
00:04:42,030 --> 00:04:44,400
So this should be pretty simple to do.

55
00:04:44,670 --> 00:04:47,070
So let's go back to Visual Studio Code.

56
00:04:47,490 --> 00:04:57,690
Now that we know that we have an object, we need to iterate over this array of courses.

57
00:04:58,140 --> 00:04:59,690
So let's start a loop.

58
00:04:59,940 --> 00:05:00,810
So for.

59
00:05:07,870 --> 00:05:15,190
Let's use a this is inside the scope of this function, so that's OK to use a we don't have to use that

60
00:05:15,190 --> 00:05:15,520
here.

61
00:05:19,120 --> 00:05:24,370
A is going to be less than horses.

62
00:05:26,490 --> 00:05:27,410
Not lenth.

63
00:05:29,030 --> 00:05:34,160
And a plus plus, that's the basic structure of a loop.

64
00:05:34,610 --> 00:05:40,400
All right, so when we are inside this loop, the first thing we are going to do is create a variable

65
00:05:40,400 --> 00:05:44,150
just for the reviews so we can work with it.

66
00:05:45,570 --> 00:05:46,680
So reviews.

67
00:05:49,250 --> 00:05:51,200
Is going to be coarsest.

68
00:05:53,680 --> 00:05:55,390
With an index of a.

69
00:05:57,060 --> 00:05:57,660
Then.

70
00:05:58,900 --> 00:06:03,490
We need to get this property, which is called reviews.

71
00:06:05,720 --> 00:06:12,380
Now we need to use a tri catch statement here, so let's use the tri.

72
00:06:15,430 --> 00:06:19,420
Then after it comes the catch.

73
00:06:23,490 --> 00:06:26,640
We need to pass an argument here for the error.

74
00:06:30,310 --> 00:06:38,020
Panel, what we're going to do here, first, we need to create our own errors, so let's go back to

75
00:06:38,020 --> 00:06:38,680
the page.

76
00:06:39,550 --> 00:06:45,300
And here we have two situations that we need to create in the code.

77
00:06:45,700 --> 00:06:51,400
So the first one is if the course doesn't have any reviews.

78
00:06:52,330 --> 00:06:53,680
So how do we do this?

79
00:06:54,910 --> 00:07:02,080
We just start an if statement, so we just created a variable for the reviews.

80
00:07:03,360 --> 00:07:06,850
And if we go back here, let's see this one.

81
00:07:07,290 --> 00:07:12,660
So this one has eight reviews, so it's an array with a length.

82
00:07:13,450 --> 00:07:16,380
This one is just an empty array.

83
00:07:17,510 --> 00:07:23,340
So we can just test if this array has any elements inside.

84
00:07:24,110 --> 00:07:27,260
So for this, we're just going to do if.

85
00:07:28,310 --> 00:07:29,120
Reviews.

86
00:07:31,980 --> 00:07:32,640
Not.

87
00:07:33,830 --> 00:07:34,400
Lenth.

88
00:07:42,130 --> 00:07:43,180
Is zero.

89
00:07:44,720 --> 00:07:49,490
Then here we are going to throw our custom error.

90
00:07:51,370 --> 00:07:52,780
So we're just going to throw.

91
00:07:55,560 --> 00:08:00,150
Let's go back there to see the exact message, so no reviews.

92
00:08:02,370 --> 00:08:05,550
That's the custom error that we need to throw.

93
00:08:07,880 --> 00:08:08,510
So here.

94
00:08:12,290 --> 00:08:13,190
We just started.

95
00:08:13,220 --> 00:08:14,270
Else if.

96
00:08:17,550 --> 00:08:19,110
And now the second rule.

97
00:08:20,780 --> 00:08:24,830
Is the chorus has less than five reviews.

98
00:08:26,760 --> 00:08:31,200
So here, if reviews start lenth, let's just copy this.

99
00:08:35,190 --> 00:08:37,200
It's less than five.

100
00:08:42,200 --> 00:08:45,560
Then we are going to throw a different error.

101
00:08:47,060 --> 00:08:51,590
Which in this case is going to be not enough reviews yet.

102
00:09:03,670 --> 00:09:05,950
So if this doesn't happen.

103
00:09:07,660 --> 00:09:16,150
It means that we have enough reviews, it's not zero, it's not less than five, so then we can start

104
00:09:16,150 --> 00:09:18,970
calculating the average reviews.

105
00:09:19,960 --> 00:09:24,490
So in order to do this, we're going to create a variable called some.

106
00:09:26,930 --> 00:09:27,680
Reviews.

107
00:09:29,140 --> 00:09:31,930
Because we need to sum all the reviews.

108
00:09:34,760 --> 00:09:39,260
And then divide it by the number of reviews so we can get the average.

109
00:09:41,370 --> 00:09:50,100
So the sum is going to start at zero and now we need to start another loop to loop through each of the

110
00:09:50,100 --> 00:09:50,680
reviews.

111
00:09:50,910 --> 00:09:56,050
So if we go back here, let's see this second course as an example.

112
00:09:56,340 --> 00:09:57,240
So we have.

113
00:09:58,180 --> 00:10:06,580
Eight reviews here from zero to seven, so we need to iterate through this array to sum all the reviews

114
00:10:06,790 --> 00:10:08,950
so then we can get the average.

115
00:10:11,730 --> 00:10:16,050
So let's just copy this structure here.

116
00:10:19,000 --> 00:10:25,600
Instead of a here, we need to use B, otherwise this is going to conflict.

117
00:10:26,630 --> 00:10:32,510
Because both are inside this for loop and inside the function.

118
00:10:32,540 --> 00:10:35,120
So here we need to change the name of the variable.

119
00:10:37,120 --> 00:10:38,740
All right, so now we have B.

120
00:10:39,760 --> 00:10:44,710
And it's not courses, not length, but reviews that length.

121
00:10:52,140 --> 00:10:54,270
So some reviews.

122
00:10:56,970 --> 00:10:58,440
It's just going to be.

123
00:11:01,140 --> 00:11:03,330
Its own value plus.

124
00:11:04,370 --> 00:11:07,340
Actually, here is not courses that reviews.

125
00:11:08,230 --> 00:11:09,730
But just reviews.

126
00:11:10,700 --> 00:11:13,310
Because that's the variable we just created.

127
00:11:14,330 --> 00:11:20,420
So now it's good, so reviews with an index of the.

128
00:11:21,390 --> 00:11:28,590
So by doing this, we just have the sum of all the reviews from this course and now.

129
00:11:30,460 --> 00:11:33,190
Let's create another variable called average.

130
00:11:37,490 --> 00:11:38,270
Reviews.

131
00:11:39,660 --> 00:11:43,140
Which is going to be the sum of reviews.

132
00:11:45,080 --> 00:11:46,670
Divided by.

133
00:11:49,090 --> 00:11:51,130
Reviews that lenth.

134
00:11:52,910 --> 00:11:55,530
All right, so what do we need to do with it?

135
00:11:55,550 --> 00:12:01,990
Let's remember that we are inside this loop iterating through each of the courses.

136
00:12:02,270 --> 00:12:09,940
So what we need to do is just adding a new property to this course.

137
00:12:10,850 --> 00:12:13,010
So let's do this here.

138
00:12:14,840 --> 00:12:17,480
So this course, let's create.

139
00:12:19,590 --> 00:12:21,750
A new property called.

140
00:12:24,780 --> 00:12:27,810
Average rating or something like this.

141
00:12:31,200 --> 00:12:34,320
And then we are just going to do this.

142
00:12:38,340 --> 00:12:46,350
But this has to be rounded to one decimal place so we can just use the two fixed method.

143
00:12:48,110 --> 00:12:59,090
And here we just want one decimal place, so if we reach this point, we are going to add a new property

144
00:12:59,090 --> 00:13:03,560
to this object, which is going to be the average rating.

145
00:13:05,010 --> 00:13:12,600
If we have any problems inside this try block, so it means that we.

146
00:13:14,010 --> 00:13:19,830
Reach these throw statements, then we need to do something else here.

147
00:13:20,890 --> 00:13:28,210
So it's going to be similar, but we just need to save those messages for the errors.

148
00:13:30,510 --> 00:13:33,780
So we can just add the.

149
00:13:35,400 --> 00:13:39,720
Error argument, which we know that by using the throe.

150
00:13:40,640 --> 00:13:46,190
Statement, I'm just going to send this to this argument.

151
00:13:47,490 --> 00:13:56,310
So after we do all this, so now we finish this for loop, we should have everything we need before

152
00:13:56,310 --> 00:13:59,610
we transform this back into a Jason.

153
00:14:02,900 --> 00:14:10,610
Let's send it to the council so we can see so here we have this crisis variable that we created.

154
00:14:13,340 --> 00:14:18,170
All right, let's save this before we refresh the page.

155
00:14:18,200 --> 00:14:20,510
Just one thing I just realized.

156
00:14:21,390 --> 00:14:24,770
I'm missing a dot lenth here.

157
00:14:26,790 --> 00:14:33,830
So B should be last than reviews that length, this would probably cause an error.

158
00:14:33,870 --> 00:14:35,670
So now I think it should be good.

159
00:14:36,580 --> 00:14:37,930
Let me save this.

160
00:14:40,420 --> 00:14:47,650
Now, refreshing the page now if we go inside each of the courses, so let's see the page, because

161
00:14:47,830 --> 00:14:54,430
we can see that now not only we have the title and reviews, but we also have a new property called

162
00:14:54,430 --> 00:14:55,690
average rating.

163
00:14:55,910 --> 00:14:59,230
So in this case, we have no reviews for this course.

164
00:14:59,470 --> 00:15:04,150
So we have just a string here that says no reviews, which is correct.

165
00:15:04,510 --> 00:15:05,550
Let's close this one.

166
00:15:05,560 --> 00:15:06,750
Let's go to the next.

167
00:15:07,300 --> 00:15:14,920
Now, we have this course with eight reviews and we can see the average rating of four point eight.

168
00:15:15,040 --> 00:15:18,190
So this is rounded to one decimal place, which is good.

169
00:15:18,640 --> 00:15:20,000
Let's go to the next one.

170
00:15:20,320 --> 00:15:26,520
So here we have the Python course with 10 reviews, average rating, four point four.

171
00:15:26,890 --> 00:15:27,630
Pretty good.

172
00:15:28,030 --> 00:15:29,360
Let's see the last one.

173
00:15:29,380 --> 00:15:34,210
So now machine learning, we have only three reviews.

174
00:15:34,540 --> 00:15:38,470
So the tax here is not enough reviews yet.

175
00:15:39,840 --> 00:15:44,670
So this is what we had to do with exercise number two.

176
00:15:45,810 --> 00:15:55,290
Actually, we are not finished yet just because we need to return a Jason, so that's the only thing

177
00:15:55,290 --> 00:15:57,000
we need to do to finish.

178
00:15:58,240 --> 00:16:03,880
So instead of doing this, let's just return the courses.

179
00:16:09,040 --> 00:16:15,460
But actually, we are going to use Jason, that string ifI.

180
00:16:20,520 --> 00:16:28,020
And then we can do like this, so our function is just going to return a Jason.

181
00:16:29,210 --> 00:16:30,860
Back so here.

182
00:16:32,350 --> 00:16:39,730
If we want to see it in the can so we can do anything we want with this function because it's just returning

183
00:16:39,730 --> 00:16:48,970
a JSON, but if we want to send it to the council, then we can just do it like this just so we can

184
00:16:48,970 --> 00:16:49,590
see it.

185
00:16:51,260 --> 00:16:57,380
So now we are sending the result of this function to the council, which is this, Jason, that it's

186
00:16:57,380 --> 00:16:59,960
returning, let's save this.

187
00:17:03,450 --> 00:17:08,520
Refreshing the page, and now we are adding a property.

188
00:17:09,520 --> 00:17:18,910
To our object, following the rules, turning it back into a Jason and returning it, so that was exercise

189
00:17:18,910 --> 00:17:19,630
number two.

190
00:17:20,730 --> 00:17:27,240
If you have any questions, you can just leave it in the Q&amp;A section, if not, just go to the next

191
00:17:27,240 --> 00:17:30,090
video so we solve exercise number three.

192
00:17:30,270 --> 00:17:31,080
I'll see you then.
