Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
WEBVTT
00:00.150 --> 00:04.910
This video is going to be the last video for this section and it's going to be a challenge video so
00:04.920 --> 00:08.670
I'm going to set up a little project for you to build and you're going to go off and write all the code
00:08.670 --> 00:10.570
required to get the job done.
00:10.590 --> 00:14.940
Obviously since we just learned about functions and a lot of their features this one is going to be
00:14.940 --> 00:16.510
focused on just that.
00:16.650 --> 00:20.610
But it also require you to recall things learned in the last section.
00:20.670 --> 00:27.780
Primarily if statements now at this point in the course we're going through these sections of just six
00:27.780 --> 00:34.890
or seven videos to get a cursory overview of each feature there is more to functions than what I've
00:34.890 --> 00:40.950
described so far but it doesn't really make sense to get into super advanced function features when
00:40.950 --> 00:45.180
there are other entire parts of the language that we still don't know things that are really important
00:45.360 --> 00:47.690
notably objects and arrays.
00:47.700 --> 00:52.470
So while there are more to functions and we will be talking about them in that course this is a great
00:52.470 --> 00:53.950
place to stop for now.
00:54.000 --> 00:59.100
Well focus on some other features and once we have a better overview of the entire language we're all
00:59.100 --> 01:01.650
focused on advanced parts of each.
01:01.650 --> 01:06.480
So for the moment let's go ahead and create a brand new file for this challenge to clean things up.
01:06.480 --> 01:10.290
I'm going to close all of the open files you're more than welcome to leave them open.
01:10.290 --> 01:16.060
The choice is yours and right here I'm going to create a file called grade hyphen.
01:16.080 --> 01:17.220
Call Gadot J.
01:17.220 --> 01:17.790
S.
01:17.880 --> 01:23.230
And as the name suggests it's going to be your job to create a grade calculator function.
01:23.230 --> 01:28.320
This grade calculator function which you'll be creating is going to take into arguments it's going to
01:28.320 --> 01:35.610
take in the students score and it's going to take in the total possible score.
01:35.610 --> 01:43.470
So if I have a test worth 20 points and I get 15 out of that 20 I would pass in 15 for these students
01:43.470 --> 01:46.570
score and the total possible score of 20.
01:46.590 --> 01:52.530
And the goal is to generate the letter grade as well as the percentage the student got.
01:52.530 --> 01:57.410
So for example if I did get a 15 out of 20 that's a 75 percent.
01:57.480 --> 02:00.630
So I would want to print a message along the lines of the following.
02:00.690 --> 02:04.450
You got a followed by the letter grade.
02:04.510 --> 02:06.410
Seventy five percent would be a C.
02:06.630 --> 02:10.670
And then we're going to include in parentheses their percentage.
02:10.680 --> 02:13.650
So in this case that would be a seventy five percent.
02:13.770 --> 02:17.500
And we can toss an exclamation mark or some other punctuation on the end.
02:17.610 --> 02:18.600
So this is the goal.
02:18.630 --> 02:24.270
Given those two inputs generate the percentage figure out what letter grade that corresponds to and
02:24.270 --> 02:26.040
return each strain.
02:26.070 --> 02:30.910
So in the end of the day we're returning a string from the function just to jog your memory.
02:31.020 --> 02:32.920
Here are the following groups.
02:32.940 --> 02:43.770
90 to 100 a Eighty to 89 be 70 to 79 C and then 4 D We got 60 to 69 and anything under 60 0 at 59 that
02:43.770 --> 02:50.610
is considered an F. it's going to be your job to use the functions the arguments the IF statements the
02:50.610 --> 02:55.130
return keyword to make all of this possible then down below.
02:55.170 --> 02:59.490
You're going to go ahead and actually call things they're going to call the function passing in the
02:59.490 --> 03:03.960
student's score and the total possible score and then going to print the string to the screen making
03:03.960 --> 03:06.950
sure it looks correct based off of the input you provided.
03:07.080 --> 03:12.630
So if I do provide 15 and 20 as the two arguments I would expect this exact message to print and then
03:12.630 --> 03:14.510
just mess around with a couple of other values.
03:14.540 --> 03:19.410
Switch up the student's score to fall into all of these ranges and watch all of the different letters
03:19.470 --> 03:20.430
show up.
03:20.430 --> 03:24.180
Now I'm not going to give you any more than this it's going to be up to you to write every single line
03:24.180 --> 03:26.190
of code using what you learned.
03:26.190 --> 03:28.440
So it takes some time to knock this one out.
03:28.470 --> 03:33.320
Test things out work through your bugs use the Internet or any previous videos if you need to.
03:33.390 --> 03:37.410
And when you're done come back click play and we'll move through things together.
03:37.440 --> 03:39.050
Take a moment to pause the video
03:43.470 --> 03:44.400
right how that one go.
03:44.400 --> 03:46.890
Hopefully you are able to create it without too much trouble.
03:46.890 --> 03:52.020
Step one is to define this function right here let you can call it whatever you want to call it.
03:52.020 --> 03:57.820
I'm going to call mine grade calc and then I'm going to set an equal to a function.
03:58.110 --> 04:02.220
I'll set up my arguments right here and I'll set up my function body down below.
04:02.220 --> 04:07.050
Now what arguments do we want we want to the first on representing the actual number of points someone
04:07.050 --> 04:11.160
got on an exam and the second one representing the total number of points.
04:11.250 --> 04:15.540
So I can call the first one score and the second one.
04:15.570 --> 04:17.970
Total score or some alternative.
04:17.970 --> 04:21.510
Once again it's not these things that are important to completing the challenge.
04:21.510 --> 04:25.810
There are infinite numbers of names that would make sense there as those argument names.
04:25.980 --> 04:28.130
Now inside of the function what do we want to do.
04:28.320 --> 04:31.490
Well the first thing we're going to do is calculate that percentage.
04:31.590 --> 04:36.730
That percentage is going to be calculated the same way regardless of what bracket they fall into.
04:36.930 --> 04:46.980
So right here I can say let percent equal and I'm going to calculate the percentage which is the score
04:47.820 --> 04:49.960
divided by the total score.
04:50.130 --> 04:51.860
And this would give us a decimal.
04:51.930 --> 04:58.760
So for 15 and 20 I would get point seventy five if I want to get a percentage like 75.
04:58.770 --> 05:03.120
All I have to do is multiply that value by 100.
05:03.120 --> 05:05.930
Now we have an actual percentage we can work with.
05:06.090 --> 05:12.290
And the next thing we're going to do is figure out what letter grade the percentage corresponds to.
05:12.480 --> 05:14.910
And we can get that done with if statements.
05:14.970 --> 05:21.670
I'm going to use a series of if and else if clauses to capture all of the various ranges and I'm going
05:21.670 --> 05:24.320
to kick things off with the letter.
05:24.990 --> 05:31.150
So right here if we're going to add if we're going to talk about the percentage in this condition.
05:31.150 --> 05:36.860
So if the percent is greater than or equal to 90.
05:36.960 --> 05:40.320
If this is true we know the person got an A.
05:40.350 --> 05:41.990
So we can go ahead and do what.
05:42.120 --> 05:45.150
Well we can go ahead and return a message.
05:45.150 --> 05:47.390
So I'm going to return.
05:47.880 --> 05:49.990
I'm going to set up a template string.
05:50.490 --> 05:54.140
And then right here we're going to start injecting those values.
05:54.150 --> 05:58.210
So you got then a.
05:58.710 --> 06:02.670
And then inside the parentheses we are going to add the actual percentage.
06:02.670 --> 06:05.460
So right here how do we inject a value into template strings.
06:05.460 --> 06:11.340
Well as we explored in the last video dollar sign open and close those curly braces and reference the
06:11.340 --> 06:14.000
variable right inside percent.
06:14.100 --> 06:15.120
Excellent.
06:15.120 --> 06:17.990
Now things you should be working for the letter A.
06:18.030 --> 06:20.730
And before we continue on we can actually test it.
06:20.730 --> 06:26.550
There's no reason to create the entire function before actually testing things out down below.
06:26.550 --> 06:29.130
Let result equal.
06:29.370 --> 06:36.150
I'm going to call grade calc to test my new function and I will pass in 15 as the score I got in 20
06:36.150 --> 06:42.980
as the total number of points then down below console the line passing into Lague the result.
06:43.170 --> 06:46.670
Let's go ahead and run this file and make sure we do get our message.
06:46.710 --> 06:48.380
Our message is actually for an A.
06:48.450 --> 06:50.600
Let's go ahead and change things right here.
06:50.610 --> 06:54.870
I'm going to give them an A by saying 19 out of 20 points.
06:54.930 --> 06:57.330
Now we can save things and actually run it.
06:57.410 --> 07:00.240
Noad grey cow couldn't J S.
07:00.330 --> 07:04.770
When I execute the file I get and you get an A 95 showing up.
07:04.770 --> 07:07.750
I also meant to add a percentage sign right after the value.
07:07.770 --> 07:09.960
So I'm going to toss that in right here.
07:09.960 --> 07:11.060
Excellent.
07:11.070 --> 07:14.030
Now we can move on to the B grade.
07:14.160 --> 07:17.070
Right now we don't have any code that handles that.
07:17.070 --> 07:17.960
So down below.
07:18.060 --> 07:19.000
What are we going to do.
07:19.140 --> 07:23.160
Well there are bunch different ways we could solve this problem but the way I'm going to go with is
07:23.160 --> 07:25.940
to add an else if clause right here.
07:26.100 --> 07:31.770
We're going to provide another condition and another block of code to run if the condition is true.
07:32.250 --> 07:33.470
Now what do we want to do here.
07:33.510 --> 07:36.340
We want to see if the grade is in this range.
07:36.420 --> 07:42.470
So we want to see if it's greater than or equal to 80 and less than or equal to 89.
07:42.480 --> 07:49.470
That means we need to add two conditions but do we really we already know that we do not have any A's
07:49.530 --> 07:50.600
showing up here.
07:50.760 --> 07:55.290
Because if the student did get an 8 it would have already been caught by the code above.
07:55.290 --> 08:02.660
So all we really need to do is check if the percent is greater than or equal to 80.
08:02.730 --> 08:04.860
You might say hey what about a ninety nine.
08:04.860 --> 08:10.120
Well don't worry about that because a 99 would have gotten caught above and the code would have returned.
08:10.200 --> 08:13.210
This condition would never actually run for a 99.
08:13.320 --> 08:17.850
So this is all we need to see if the student got a B.
08:17.850 --> 08:19.380
Now what are we going to do here.
08:19.380 --> 08:23.370
Well what we're going to do in here is return a message and the message is going to be so similar to
08:23.370 --> 08:24.240
the one above.
08:24.480 --> 08:30.760
If we copy and paste it down below all we really do is change a letter we change a to b.
08:30.960 --> 08:32.570
Well this gets the job done.
08:32.670 --> 08:37.330
It's not the most efficient way because we have this string in many different places.
08:37.350 --> 08:42.360
So if I wanted to change the sentence For example I would have to go through and change every single
08:42.670 --> 08:43.490
enough of it.
08:43.690 --> 08:46.540
Well this is a perfectly fine way to get the job done.
08:46.540 --> 08:48.970
I'm going to show you an alternative solution.
08:48.970 --> 08:51.790
First off though let's go ahead and actually get a B.
08:52.000 --> 08:55.010
I'll give myself 17 out of 20 points.
08:55.210 --> 08:58.000
I'll rerun things over in the terminal and right here.
08:58.000 --> 08:58.820
I do have.
08:58.840 --> 09:01.140
You've got to be at 85 percent.
09:01.240 --> 09:06.370
Now we're going to do before we add the other letter grades is restructure things just a little bit
09:06.970 --> 09:07.820
up above.
09:07.960 --> 09:12.270
But we're going to do is create let And I'm going to call this one.
09:12.280 --> 09:14.870
Let letter grade.
09:15.070 --> 09:20.530
And I am going to set it equal to and we can just started off as an empty string or undefined or something
09:20.530 --> 09:21.840
along those lines.
09:21.910 --> 09:27.900
What we're going to do is change the value of letter grade inside of the if code blocks.
09:27.940 --> 09:33.670
Then after the if code blocks we will return a template string that's going to make sure that we don't
09:33.670 --> 09:36.540
have duplicate code inside of our function.
09:36.550 --> 09:40.560
So let's see what that would look like right here inside of this if statement.
09:40.720 --> 09:48.070
We're going to set letter grade equal to and then we can actually remove the return statement right
09:48.070 --> 09:50.400
here in this code block.
09:50.410 --> 09:57.280
We're going to set a letter grade equal to B then we can remove this return statement right here.
09:57.280 --> 09:58.740
I'm actually going to cut it out.
09:58.780 --> 10:02.750
Instead of deleting it completely because we're going to use it just down below.
10:03.130 --> 10:07.820
After our if statement but still inside the function we're going to paste that line.
10:07.960 --> 10:13.990
Now we are returning the string and all we have to do is replace the static letter B with the value
10:13.990 --> 10:15.250
of a letter grade.
10:15.250 --> 10:23.170
So right here dollar sign curly braces whoops marry the dollar sign of curly braces and inside of there
10:23.170 --> 10:27.080
we reference the letter grade variable.
10:27.220 --> 10:32.110
So hopefully you're able to see why this is preferred instead of having this sentence and all sorts
10:32.110 --> 10:33.160
of different places.
10:33.220 --> 10:34.390
We have it in just one.
10:34.420 --> 10:37.280
Making it much easier to update and maintain.
10:37.300 --> 10:44.980
Now we can continue on with our statement else if we're going to check if the percent is greater than
10:44.980 --> 10:46.570
or equal to 70.
10:46.690 --> 10:52.620
And if it is you know the student has a C so letter grade equals C.
10:52.630 --> 10:59.680
Next up LCF Let's go ahead and figure out that they got a D if the percent is greater than or equal
10:59.680 --> 11:05.370
to 60 then we know they got a D right here a letter grade equals D.
11:05.650 --> 11:08.540
And the final thing we're going to check is if they got an F.
11:08.590 --> 11:12.090
Now we could use an else f to see if their grade was above zero.
11:12.160 --> 11:18.090
But if they didn't get an A B C or D We know they got an F so we could actually just use else.
11:18.310 --> 11:24.160
This code is only ever going to run if they do not fall in any of the other ranges so in here a letter
11:24.160 --> 11:28.060
grade equals f just like this.
11:28.060 --> 11:32.450
Now that we have the if statements in place we can actually test things out.
11:32.470 --> 11:35.680
I'm going to switch it up from a 17 to a 15.
11:35.680 --> 11:37.900
This should give the student a C..
11:37.900 --> 11:42.000
Let's go ahead and save the file and rerun things.
11:42.010 --> 11:43.460
We got our file right here.
11:43.540 --> 11:47.300
Ivry run it and I see you got to see 75 percent printing.
11:47.320 --> 11:52.720
So our new system of setting letter grade up above and just returning a single template string down
11:52.720 --> 11:54.490
below is clearly working.
11:54.740 --> 11:55.870
Let's switch up the great again.
11:55.870 --> 12:03.120
I'm going to go to a 13 which should give them a D and you can see right here you've got a D at 65 percent.
12:03.250 --> 12:07.930
And then I'll switch things up to a 9 which should be an F. I'll run it.
12:07.960 --> 12:10.870
You got an F at forty five percent.
12:10.870 --> 12:17.140
So this is what I wanted you to do for the challenge as I've mentioned plenty of times before the individual
12:17.140 --> 12:18.640
details aren't important.
12:18.640 --> 12:21.240
You might find that your code doesn't look just like mine.
12:21.280 --> 12:25.960
If I were to write this five times I'm sure I could come up with a different solution each time without
12:25.960 --> 12:27.370
even really thinking about it.
12:27.490 --> 12:32.890
What is important though is that given the inputs you got the correct output in order to get the correct
12:32.890 --> 12:38.060
output you would have had to use things like if statements and function arguments and template strings
12:38.350 --> 12:42.490
maybe a didn't use template strings maybe used regular strings with concatenation.
12:42.520 --> 12:43.360
That's fine too.
12:43.420 --> 12:48.820
But in that case I do challenge you to take a quick moment to upgrade to use templates strings as there
12:48.820 --> 12:51.140
are a lot nicer to work with.
12:51.160 --> 12:51.670
All right.
12:51.670 --> 12:55.290
That's going to be it for this challenge and that's also it for this section.
12:55.300 --> 12:58.720
We have a basic understanding of how functions work in javascript.
12:58.760 --> 13:03.430
What we're going to do is move on to the next section and take a look at a couple of other types that
13:03.430 --> 13:07.280
are essential to working with javascript in the real world.
13:07.330 --> 13:08.760
I'm very excited to get to that.
13:08.830 --> 13:12.850
So let's go ahead and jump right in to the section intro for that one.
18002
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.