Everyone, let's see the solution for exercise number two.
So, number one, create a function to convert Celsius to Fahrenheit.
So I'm just going to copy all this so I can use it in my code.
So go into visual studio code.
Like I said, I already created this new jass file, included the link at the bottom of this HTML file,
and now I'm just going to solve the exercises.
So first I'm going to create a function.
And this is going to be the name of my function.
This is the only thing I need than the formula is also going to be useful.
So function.
Let's just adjust the name so Celsius.
To.
Fahrenheit is going to be the name of my function, so open and close parentheses, open and close,
curly braces inside here is going to be the code for our function.
Now we are going to need an argument, which is going to be the Celsius temperature.
So then we can just copy the formula, so we are going to create a variable called, say, Farenheit.
Then here we are going to use the Celsius.
Argument like this, so then we would have it calculated and then all we need to do is just returning
the result, which is going to be the temperature and Fahrenheit, then we can do anything we want with
this function.
We can use it to send a valid to the console.
We can use it to.
Use it in another calculation or we can send to the boxes of our small file, which is what we are going
to do now.
So let's go back there to check the IDs of the boxes where we are going to get the Celsius temperature
and where we need to place the results.
So using the inspector, we can see that we have Celsius, one Celsius.
Two and probably Celsius three and the same thing for the Fahrenheit boxes, so here we have far one.
Then we have far, too.
And.
Far three, so these are the boxes that we're going to have to use.
So let me just copy this.
I'm just going to leave it here.
All right, so the first thing we need to do here is create variables to get the Celsius temperature
because we are going to have to convert it to a number before we use it in this function.
So if we try to do everything in one line, this is going to be a bit messy.
So let's just create a variable called Celsius one.
And then this is going to be document that that element by ID.
Then we can just use this.
But instead of two is going to be Celsius one.
And then we also want to convert it, we know that we don't have any decimal places, but since this
is a temperature, it's possible that we have temperatures with decimal places.
So let's do pass float, which is going to be safer.
So pass floats, so now we have the temperature and this has been converted to a no.
Just one thing I forgot, we still need to use the Internet age HTML property.
So now this would be correct.
Before we move on, let's just save this.
And let's go to the concert.
Let's try to get the Celsius one variable, as you can see, we were able to get the value here.
So now we just have to use it as an argument for our function.
So now we can just do document.
Not get aliment by ID.
Now it's going to be far.
One, which is the element where we want to place the result so that in our age, tml and now what we
are going to do here is just executing our function.
And using this.
Variable as an argument, so it is going to get this thirty eight, use it in the formula, return the
value we want, so the value is going to be replaced here and it is going to go inside that element.
Before moving on, let's refresh the page and see if this is working.
So now we can see that this is working.
Pretty cool now that we have a function, this is going to be really simple to repeat, we just spend
a little time preparing the code, but now it's going to be as easy as just changing this to two and
two here as well here as well.
Another one for.
The last element, so three.
Three.
Three and three.
Saving this refreshing the page.
We just used our function three times to make three temperature conversions.
Pretty cool.
No, to the array below is the classification of students in a contest, it is order from lowest to
highest grade.
So we need a place inside this element, the three best students.
So if this is ordered from lowest to highest, we just need to get the last three students.
How do we do this in JavaScript?
First of all, let's copy this.
So this is number two.
Let's copy this variable.
Now let's get the idea of that element that we need to place the result.
So this is best student.
OK, so we can just copy all this.
And now let's replace the ID for our best students, so inside that element, we are going to place
our array, but we are going to do a slicing so we know that we can do classification that slice and
then we can use negative numbers.
So if we do, minus one is just going to get the last.
If we want the last three, we can just do minus three and then just omit the second value.
You can include the comma or not if you don't want.
I think in the example in the lesson, I just showed it like this, so let's see if this is working.
Refreshing the page.
And now we're seeing the last three students, which is exactly what we wanted.
OK, number three, let's just copy this object so we can work with it.
Let me go to Visual Studio Code.
I'm just going to start.
Number three here and just copy the objects.
So now let's go back there to see what we need to do.
So show the course title in the box below.
This should be pretty simple.
Let's just get the IDs.
So we have the course title box.
I'm just going to leave them here.
We also have the main category element.
So this is going to be four letter B.
And four letter C.
We have the reviews, Five Stars ID.
OK, so first one, pretty simple to do, let's just use this one more time.
I know we are repeating this method a lot of times, but this is good to practice.
This is good to getting used to using this and typing faster.
So first one, let's just replace it for the course title.
And now we know that we can just do course that.
If you want, you can use the Brace's notation by doing this, so Brace's.
And then tidal, this is going to work the same way, but since our properties are following the variable
naming rules, we can use the DOT notation, which not only is more simple, but it's actually more
used by JavaScript programmers.
So by doing this, let's see if this is right.
Refreshing the page going down, we can see the title of the course here.
Now the main category in the next box.
So let's just get this.
Now, the idea of the element is main category now this is going to be just as easy as the first one,
we just need to go to the categories.
And now we need to get the first element so the index is zero.
By doing this.
We are getting the main category, so let's see, we're just going to need to do some calculation.
This is going to be easy as well.
But for this, I would like to create a method, so I'm going to create a method for this object, which
is going to be for the total number of reviews, because we have all this.
Let's say we need to get the number of reviews for other things.
Now, we need it for for this calculation.
So instead of calculating again over and over again every time, I'm just going to create a method so
we can do this.
So the method is going to be named Toto.
Reviews, so we are going to assign a function to it.
And then we can just do this, that.
Five star reviews.
Actually, this is not going to work because these properties are not following the variable naming
rules, so we cannot use the dot notation.
So we have to do it like this.
With the braces and quotation marks.
OK, now this should be good, so we need to get five star reviews.
All the four star reviews.
Three stars, two stars and one stars.
So one.
Three.
Four and five, so now every time we need to use the total number of reviews for anything, we just
have a method that is going to do this for us.
So let's just return this.
And now to see if this is working before moving ahead, we can print it to the council so we can use
course.
Total reviews, open and close parentheses, because this is a method.
So let's see if this is working.
Refreshing the page going to the con so we can see that we have the total number of reviews really good.
Now, let's just copy this.
Let's get the idea.
So the idea is.
With these five stars where we need to place the result and inside this elements, we are just going
to do.
Of course.
Then we're going to get the number of five star reviews, so we need to use Brace's because of how the
property was named and then we just need to.
Divided by the total reviews.
OK, so by doing this.
This is going to be part of what we need to do, so let's just save this just to see if this is working.
OK, this is working, but now we just need to multiply it by a hundred so we can get the percentage
because now we have a number from zero to one and we need to show it like 75 percent, 20 percent of
stuff like this.
So we just.
Need to apply a little bit more calculation and formatting, so for this, I think it's better to create
a variable, do it separately.
So I'm going to create a variable called percentage.
Then let's get all this.
And replace it here.
And down here, we can just.
Use this variable, so now we can do the rest of the stuff we need to do, so now we can just multiply
it by 100.
Then we can just enclose everything inside parentheses.
And before going ahead, let's see what happened, let's save this, just see if this is working.
So I prefer to do this step by step, go back there, see if this is working before I move ahead.
So refreshing the page, let's go down here, we can see that.
This is now closer to what we need now we just need to round this number to zero decimal places.
We know that we can do this by just.
Applying the round function so round.
Actually, I just forgot that round is a method of the math.
Object, so now it should be right.
Now we have seven, 75, five.
Which is good if you want to make this better.
You can even get this and convert it into a string, so to string.
And then you can concatenated we have a.
Percentage sign.
So by doing this.
Now we can see that seventy five percent of our reviews are five star reviews, pretty cool.
This is done.
Now, this video is almost exceeding the limit.
So I'm going to stop here.
And in the next video, I'm going to solve a number.
I'll see you then.