Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
This video is going to be the last video for this section and it's going to be a challenge video so
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
required to get the job done.
Obviously since we just learned about functions and a lot of their features this one is going to be
focused on just that.
But it also require you to recall things learned in the last section.
Primarily if statements now at this point in the course we're going through these sections of just six
or seven videos to get a cursory overview of each feature there is more to functions than what I've
described so far but it doesn't really make sense to get into super advanced function features when
there are other entire parts of the language that we still don't know things that are really important
notably objects and arrays.
So while there are more to functions and we will be talking about them in that course this is a great
place to stop for now.
Well focus on some other features and once we have a better overview of the entire language we're all
focused on advanced parts of each.
So for the moment let's go ahead and create a brand new file for this challenge to clean things up.
I'm going to close all of the open files you're more than welcome to leave them open.
The choice is yours and right here I'm going to create a file called grade hyphen.
Call Gadot J.
S.
And as the name suggests it's going to be your job to create a grade calculator function.
This grade calculator function which you'll be creating is going to take into arguments it's going to
take in the students score and it's going to take in the total possible score.
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
score and the total possible score of 20.
And the goal is to generate the letter grade as well as the percentage the student got.
So for example if I did get a 15 out of 20 that's a 75 percent.
So I would want to print a message along the lines of the following.
You got a followed by the letter grade.
Seventy five percent would be a C.
And then we're going to include in parentheses their percentage.
So in this case that would be a seventy five percent.
And we can toss an exclamation mark or some other punctuation on the end.
So this is the goal.
Given those two inputs generate the percentage figure out what letter grade that corresponds to and
return each strain.
So in the end of the day we're returning a string from the function just to jog your memory.
Here are the following groups.
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
is considered an F. it's going to be your job to use the functions the arguments the IF statements the
return keyword to make all of this possible then down below.
You're going to go ahead and actually call things they're going to call the function passing in the
student's score and the total possible score and then going to print the string to the screen making
sure it looks correct based off of the input you provided.
So if I do provide 15 and 20 as the two arguments I would expect this exact message to print and then
just mess around with a couple of other values.
Switch up the student's score to fall into all of these ranges and watch all of the different letters
show up.
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
of code using what you learned.
So it takes some time to knock this one out.
Test things out work through your bugs use the Internet or any previous videos if you need to.
And when you're done come back click play and we'll move through things together.
Take a moment to pause the video
right how that one go.
Hopefully you are able to create it without too much trouble.
Step one is to define this function right here let you can call it whatever you want to call it.
I'm going to call mine grade calc and then I'm going to set an equal to a function.
I'll set up my arguments right here and I'll set up my function body down below.
Now what arguments do we want we want to the first on representing the actual number of points someone
got on an exam and the second one representing the total number of points.
So I can call the first one score and the second one.
Total score or some alternative.
Once again it's not these things that are important to completing the challenge.
There are infinite numbers of names that would make sense there as those argument names.
Now inside of the function what do we want to do.
Well the first thing we're going to do is calculate that percentage.
That percentage is going to be calculated the same way regardless of what bracket they fall into.
So right here I can say let percent equal and I'm going to calculate the percentage which is the score
divided by the total score.
And this would give us a decimal.
So for 15 and 20 I would get point seventy five if I want to get a percentage like 75.
All I have to do is multiply that value by 100.
Now we have an actual percentage we can work with.
And the next thing we're going to do is figure out what letter grade the percentage corresponds to.
And we can get that done with if statements.
I'm going to use a series of if and else if clauses to capture all of the various ranges and I'm going
to kick things off with the letter.
So right here if we're going to add if we're going to talk about the percentage in this condition.
So if the percent is greater than or equal to 90.
If this is true we know the person got an A.
So we can go ahead and do what.
Well we can go ahead and return a message.
So I'm going to return.
I'm going to set up a template string.
And then right here we're going to start injecting those values.
So you got then a.
And then inside the parentheses we are going to add the actual percentage.
So right here how do we inject a value into template strings.
Well as we explored in the last video dollar sign open and close those curly braces and reference the
variable right inside percent.
Excellent.
Now things you should be working for the letter A.
And before we continue on we can actually test it.
There's no reason to create the entire function before actually testing things out down below.
Let result equal.
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
as the total number of points then down below console the line passing into Lague the result.
Let's go ahead and run this file and make sure we do get our message.
Our message is actually for an A.
Let's go ahead and change things right here.
I'm going to give them an A by saying 19 out of 20 points.
Now we can save things and actually run it.
Noad grey cow couldn't J S.
When I execute the file I get and you get an A 95 showing up.
I also meant to add a percentage sign right after the value.
So I'm going to toss that in right here.
Excellent.
Now we can move on to the B grade.
Right now we don't have any code that handles that.
So down below.
What are we going to do.
Well there are bunch different ways we could solve this problem but the way I'm going to go with is
to add an else if clause right here.
We're going to provide another condition and another block of code to run if the condition is true.
Now what do we want to do here.
We want to see if the grade is in this range.
So we want to see if it's greater than or equal to 80 and less than or equal to 89.
That means we need to add two conditions but do we really we already know that we do not have any A's
showing up here.
Because if the student did get an 8 it would have already been caught by the code above.
So all we really need to do is check if the percent is greater than or equal to 80.
You might say hey what about a ninety nine.
Well don't worry about that because a 99 would have gotten caught above and the code would have returned.
This condition would never actually run for a 99.
So this is all we need to see if the student got a B.
Now what are we going to do here.
Well what we're going to do in here is return a message and the message is going to be so similar to
the one above.
If we copy and paste it down below all we really do is change a letter we change a to b.
Well this gets the job done.
It's not the most efficient way because we have this string in many different places.
So if I wanted to change the sentence For example I would have to go through and change every single
enough of it.
Well this is a perfectly fine way to get the job done.
I'm going to show you an alternative solution.
First off though let's go ahead and actually get a B.
I'll give myself 17 out of 20 points.
I'll rerun things over in the terminal and right here.
I do have.
You've got to be at 85 percent.
Now we're going to do before we add the other letter grades is restructure things just a little bit
up above.
But we're going to do is create let And I'm going to call this one.
Let letter grade.
And I am going to set it equal to and we can just started off as an empty string or undefined or something
along those lines.
What we're going to do is change the value of letter grade inside of the if code blocks.
Then after the if code blocks we will return a template string that's going to make sure that we don't
have duplicate code inside of our function.
So let's see what that would look like right here inside of this if statement.
We're going to set letter grade equal to and then we can actually remove the return statement right
here in this code block.
We're going to set a letter grade equal to B then we can remove this return statement right here.
I'm actually going to cut it out.
Instead of deleting it completely because we're going to use it just down below.
After our if statement but still inside the function we're going to paste that line.
Now we are returning the string and all we have to do is replace the static letter B with the value
of a letter grade.
So right here dollar sign curly braces whoops marry the dollar sign of curly braces and inside of there
we reference the letter grade variable.
So hopefully you're able to see why this is preferred instead of having this sentence and all sorts
of different places.
We have it in just one.
Making it much easier to update and maintain.
Now we can continue on with our statement else if we're going to check if the percent is greater than
or equal to 70.
And if it is you know the student has a C so letter grade equals C.
Next up LCF Let's go ahead and figure out that they got a D if the percent is greater than or equal
to 60 then we know they got a D right here a letter grade equals D.
And the final thing we're going to check is if they got an F.
Now we could use an else f to see if their grade was above zero.
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.
This code is only ever going to run if they do not fall in any of the other ranges so in here a letter
grade equals f just like this.
Now that we have the if statements in place we can actually test things out.
I'm going to switch it up from a 17 to a 15.
This should give the student a C..
Let's go ahead and save the file and rerun things.
We got our file right here.
Ivry run it and I see you got to see 75 percent printing.
So our new system of setting letter grade up above and just returning a single template string down
below is clearly working.
Let's switch up the great again.
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.
And then I'll switch things up to a 9 which should be an F. I'll run it.
You got an F at forty five percent.
So this is what I wanted you to do for the challenge as I've mentioned plenty of times before the individual
details aren't important.
You might find that your code doesn't look just like mine.
If I were to write this five times I'm sure I could come up with a different solution each time without
even really thinking about it.
What is important though is that given the inputs you got the correct output in order to get the correct
output you would have had to use things like if statements and function arguments and template strings
maybe a didn't use template strings maybe used regular strings with concatenation.
That's fine too.
But in that case I do challenge you to take a quick moment to upgrade to use templates strings as there
are a lot nicer to work with.
All right.
That's going to be it for this challenge and that's also it for this section.
We have a basic understanding of how functions work in javascript.
What we're going to do is move on to the next section and take a look at a couple of other types that
are essential to working with javascript in the real world.
I'm very excited to get to that.
So let's go ahead and jump right in to the section intro for that one.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.