Hey, everyone, let's get started with exercise number two, so the Jason below represents a list of
courses.
Each course has a title and an array of reviews create a function to pass the Jason, add another property
for the average rating of each course, then return a new JSON with the updated object.
So we also have some additional rules down here.
But let's get started with the first part of the exercise.
So let's just copy this.
Let's pretend we just received this data from an external source like an API.
So going back to Visual Studio Code, let's base this.
We have some RAD underlines here, which indicates errors.
But this is just because we broke lines.
We can just move everything into a single line like this.
And now we are good.
All right, so let's create our function, so I'm just going to create a new function here called maybe
average.
Reviews or something like this?
Now we need to pass an argument which is going to be our Jason, so for this, since we are simulating
a request to an API, I'm just going to call it data.
So the data that we are going to pass as an argument is just going to be this, Jason, that we have
right here.
So the first thing we're going to do when we start dysfunction is creating a variable.
I'm going to call it causes.
And we shouldn't worry about any conflict between these two because this variable here is inside the
local scope of this function.
So this is completely fine.
So, of course, this is going to be Jason.
That pass.
And then we are going to pass the data.
So if we do this, we should have our data as a JavaScript object.
So let's do a consult that clock just to see if everything's working, so let's send the courses, which
again, it's not going to be this one, but this one inside the local scope of the function.
And we also need to execute the function on page load.
So right here we are just creating this function, we still need to execute it, so let's do it right
here.
And as an argument for this function, we are just going to pass the courses variable, which is the
one in the global scope.
So this one, it is going to be passed as an argument of this function.
So everything should work if we do like this.
Let's save this.
Let's go back to our page.
I'm going to refresh the page.
If I go to the inspector, let's go to the consul, and now we are seeing an array.
So every element of this array is a course which has title and reviews.
So, again, title reviews.
Pretty cool, so we're getting somewhere here.
All right, so now we need to add the average rating for each of the courses, but we have the second
part of the exercise, which is using the tri catch intro to comply with the following rules.
So if the course doesn't have any reviews, so instead of showing zero, we need to show no reviews.
So then we are not going to show a number, but we are going to show a string that says no reviews if
the course has less than five reviews.
So from one to five reviews, we should show not enough reviews yet.
So we are still not going to show the average.
Otherwise the property should receive the average rating of the course rounded to one decimal place.
So this should be pretty simple to do.
So let's go back to Visual Studio Code.
Now that we know that we have an object, we need to iterate over this array of courses.
So let's start a loop.
So for.
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
here.
A is going to be less than horses.
Not lenth.
And a plus plus, that's the basic structure of a loop.
All right, so when we are inside this loop, the first thing we are going to do is create a variable
just for the reviews so we can work with it.
So reviews.
Is going to be coarsest.
With an index of a.
Then.
We need to get this property, which is called reviews.
Now we need to use a tri catch statement here, so let's use the tri.
Then after it comes the catch.
We need to pass an argument here for the error.
Panel, what we're going to do here, first, we need to create our own errors, so let's go back to
the page.
And here we have two situations that we need to create in the code.
So the first one is if the course doesn't have any reviews.
So how do we do this?
We just start an if statement, so we just created a variable for the reviews.
And if we go back here, let's see this one.
So this one has eight reviews, so it's an array with a length.
This one is just an empty array.
So we can just test if this array has any elements inside.
So for this, we're just going to do if.
Reviews.
Not.
Lenth.
Is zero.
Then here we are going to throw our custom error.
So we're just going to throw.
Let's go back there to see the exact message, so no reviews.
That's the custom error that we need to throw.
So here.
We just started.
Else if.
And now the second rule.
Is the chorus has less than five reviews.
So here, if reviews start lenth, let's just copy this.
It's less than five.
Then we are going to throw a different error.
Which in this case is going to be not enough reviews yet.
So if this doesn't happen.
It means that we have enough reviews, it's not zero, it's not less than five, so then we can start
calculating the average reviews.
So in order to do this, we're going to create a variable called some.
Reviews.
Because we need to sum all the reviews.
And then divide it by the number of reviews so we can get the average.
So the sum is going to start at zero and now we need to start another loop to loop through each of the
reviews.
So if we go back here, let's see this second course as an example.
So we have.
Eight reviews here from zero to seven, so we need to iterate through this array to sum all the reviews
so then we can get the average.
So let's just copy this structure here.
Instead of a here, we need to use B, otherwise this is going to conflict.
Because both are inside this for loop and inside the function.
So here we need to change the name of the variable.
All right, so now we have B.
And it's not courses, not length, but reviews that length.
So some reviews.
It's just going to be.
Its own value plus.
Actually, here is not courses that reviews.
But just reviews.
Because that's the variable we just created.
So now it's good, so reviews with an index of the.
So by doing this, we just have the sum of all the reviews from this course and now.
Let's create another variable called average.
Reviews.
Which is going to be the sum of reviews.
Divided by.
Reviews that lenth.
All right, so what do we need to do with it?
Let's remember that we are inside this loop iterating through each of the courses.
So what we need to do is just adding a new property to this course.
So let's do this here.
So this course, let's create.
A new property called.
Average rating or something like this.
And then we are just going to do this.
But this has to be rounded to one decimal place so we can just use the two fixed method.
And here we just want one decimal place, so if we reach this point, we are going to add a new property
to this object, which is going to be the average rating.
If we have any problems inside this try block, so it means that we.
Reach these throw statements, then we need to do something else here.
So it's going to be similar, but we just need to save those messages for the errors.
So we can just add the.
Error argument, which we know that by using the throe.
Statement, I'm just going to send this to this argument.
So after we do all this, so now we finish this for loop, we should have everything we need before
we transform this back into a Jason.
Let's send it to the council so we can see so here we have this crisis variable that we created.
All right, let's save this before we refresh the page.
Just one thing I just realized.
I'm missing a dot lenth here.
So B should be last than reviews that length, this would probably cause an error.
So now I think it should be good.
Let me save this.
Now, refreshing the page now if we go inside each of the courses, so let's see the page, because
we can see that now not only we have the title and reviews, but we also have a new property called
average rating.
So in this case, we have no reviews for this course.
So we have just a string here that says no reviews, which is correct.
Let's close this one.
Let's go to the next.
Now, we have this course with eight reviews and we can see the average rating of four point eight.
So this is rounded to one decimal place, which is good.
Let's go to the next one.
So here we have the Python course with 10 reviews, average rating, four point four.
Pretty good.
Let's see the last one.
So now machine learning, we have only three reviews.
So the tax here is not enough reviews yet.
So this is what we had to do with exercise number two.
Actually, we are not finished yet just because we need to return a Jason, so that's the only thing
we need to do to finish.
So instead of doing this, let's just return the courses.
But actually, we are going to use Jason, that string ifI.
And then we can do like this, so our function is just going to return a Jason.
Back so here.
If we want to see it in the can so we can do anything we want with this function because it's just returning
a JSON, but if we want to send it to the council, then we can just do it like this just so we can
see it.
So now we are sending the result of this function to the council, which is this, Jason, that it's
returning, let's save this.
Refreshing the page, and now we are adding a property.
To our object, following the rules, turning it back into a Jason and returning it, so that was exercise
number two.
If you have any questions, you can just leave it in the Q&A section, if not, just go to the next
video so we solve exercise number three.
I'll see you then.