All right, so continuing with our third list of exercises, we were just about to start solving number
three, so this is going to be more or less similar to the first exercise because we can only start
doing anything after pressing the calculate button.
So this button has the idea of calculate.
So let's go back there.
And let's just get this cold.
And based it down here, this time, the baton is going to be the calculate button, then we are going
to start a function.
And here we are just going to get the two grades and the absences.
So grade one, let's go back there to see yds.
So this is grade one, grade two and absences.
So the idea is grade one.
Now, let's do the same thing.
For a grade two.
And for the.
Absents.
Now, before we move ahead, we should test if all of the information have been entered, so let's start
an if statement and this time we're going to use the OR operator, because if grade one is an empty
string.
Or great, too, is an empty string.
Or.
Absences is an empty string.
So if by using the or operator, if one of these tests return true, we're going to enter here.
So this means that something is wrong so we can just send an alert saying that.
All fields must be.
Field, so let's just test this real quick, refreshing the page.
Now, if I click calculate, I'm seeing the alert, so nothing is going to happen if I feel this one
and this one and leave the third one blank and click calculate again, nothing is going to happen.
Only when we type all the information.
When we fill all the feud's, then something is going to happen.
So this is what we need to do inside else statement.
So inside here, the first thing we are going to do is create a variable for the average.
So average is going to be inside parentheses grade one plus grade two.
Divided by two.
And another variable for the present.
So the presence has to be higher than 70 percent, which means that we're going to get the total number
of classes, which is 20 subtract by the absences.
So let's say the person had one absence in this case.
This is going to return in 19.
So the person went to the classes 19 times.
So if we divide this by the total number of classes.
Then we are going to have the percentage of presence, just one thing to pay attention.
We should never repeat values like this, because if we are using this for another class, which has
twenty five or maybe 15 classes, then we are going to look throughout our code and replace this information
everywhere.
So to avoid this, we should create a variable for the total.
Classis, this is in this case, this is 20.
So now we can just use the variable here and the variable here, so if this changes to a different number,
we can just change it here and we won't have to be concerned about the rest of the code.
All right.
So pretty cool.
Now, we already have the presence, which should be higher than 70 percent.
So now let's start doing the tests.
But now, before moving ahead, remember, we should always test what we're doing while we are doing
to avoid mistakes, because maybe at the end we just write all the code and if it doesn't work, is
going to be harder to spot what was the error at the end?
So now let's just check if the average and presents calculations are correct.
So let's do console.
That log for the average.
And also for the presence.
So remember, in the beginning of the chorus, I told you that console, that log was our best friend
because we can use it to test our code while we write it.
So let's do this.
Let's refresh the page and let's try eight and six.
And let's try, I don't know, 10 absences, let's hit calculate, let's go to the council.
So now we have a very strange result in the console, JavaScript is telling us that the average between
eight and six is forty three.
So where does this come from?
Well, to test this out, let's just go back here.
And let's.
Get this statement and run it in the console to see what is going on.
So if we do this and press enter, we can see that we are getting a string.
So probably what JavaScript is doing is just at first we are using a plus sign so JavaScript knows what
to do with strings when we use plus signs.
So it is just getting eight and concatenating with six.
So we are getting eighty six.
But when we divide this eighty six string by two, JavaScript starts to get a bit confused and just
tries to do what it thinks is the correct thing to do.
So just convert this string to number and calculate the average.
So eighty six divided by two is forty three.
So as you can see, remember when we spoke about type conversions, I told you that you should always
be careful about this because sometimes we may have unexpected results.
So in this case we are having a number back.
But this number is far from being correct, so we should not forget to do this.
So when we are getting the first grade, we should apply the pass float at first.
So here we have it and also my apologies, because I forgot to do it in the first exercise, so here
it worked.
But this was actually because I was lucky because I should always pay attention and convert these values.
So here.
We should have done it like this as well.
All right, so going down here now, saving this, let's refresh the page first, let's test if this
one is still working, so.
Thirty five degrees Celsius, it's ninety five Fahrenheit, this is fine, and now this one, let's
try the same number.
So eight and six and 10 absences when we heat calculate now we have the correct values, seven and zero
point five.
So now we are safe to move ahead.
So now we have multiple ways of solving this.
We can use and and or operators.
We can do nested conditionals.
It's really up to you.
Just try not to write too many lines of code.
So maybe your solution wasn't the same as mine, but if your solution is working as well, it should
be fine.
So the way I'm going to solve this is the following.
I'm going to start an IF statement and first I'm going to test if the student has failed for both reasons.
So this means that the.
Average is.
Less than six point five.
And at the same time, the presence.
It's less than zero point seven.
So in this case, I know that the student has failed for both reasons.
So I'm just going to start a variable out here just so I don't have to use the VA keyword all the time.
So now without.
It's going to be the student has failed.
Because of.
Insufficient grade and too many absences, if you want, you can try to write a better text.
This is just an example.
So in this case, I know that the student has failed for both reasons.
So now I'm just going to start and else if.
So this time I'm just going to repeat one of these tests so I know this sounds a bit counterintuitive,
why am I repeating this?
This is just because we know that if this returns true, so both are true.
We are going inside here and JavaScript is just going to ignore all the rest of the structure.
So if we are getting to this point, it means that one of these returned.
True.
So if the average is less than six point five, it means that the presence was good because otherwise
we would have stopped here.
So in this case, the result.
It's going to be the student has failed because of insufficient grade.
So, again, how do we know that it wasn't because of both reasons, because if so, we would have stopped
here and JavaScript would have ignored all the rest of the structure.
So now just another else, if that's going to be very similar, but this time we are going to use the
presence.
So if we fall here.
I just forgot the semicolon here and here aswell.
So if we fall here, it means that the student has failed because of too many absences.
How do we know it, because if it was because of both reasons, we would have stopped here.
Then if we don't go inside any of these.
If statements, we know that the student has been approved, so result.
It's going to be.
This student has been approved now the only thing we need to do is just.
Printing this value inside that element.
So let's check what is that element?
This one has the idea of result.
So now inside it, we are just going to print the result.
Now, let's make some tests to see if this is working.
Now, let's try five, five and 10.
So the student is going to fail for both reasons, let's test this out so now we can see that this is
working.
Now, let's do only two absences now.
It was only because of insufficient grade.
Now let's try 10 again.
But this time, two good grades.
So this time it was only because of the absences of now to good grades and a low number of absences,
and this time this student has been approved.
So this is the way I solved it.
If you did it differently, if you think your way of doing it was more efficient than mine, you can
just share it in the Q&A section.
Let's move on.
There's one thing we didn't do.
We did not validate this.
These numbers like.
Grades from zero to 10, absences from zero to 20, if I try 55 here.
Our program is just accepting this, but I'm not going to go into this now because this video is getting
a bit too long, if you want to try to do this validation, you can do it just as an extra challenge
later.
And of course, we might talk about data validation.
So let's just leave it like this for now.
And now, number four, I honestly spent a bit more time than expected with number three.
So now let's stop this video.
And in the next part, we are going to solve number four.
I'll see you then.