Loading
Lesson 32
Courses / JavaScript for Complete Beginners
Solution to Mapping a Score to Letter Grade in JavaScript

Video Transcript

Let me first have the letter grade mapping table right here on the right hand side so we can have a reference. Now the first condition right here is going to be what? Let's say we have right here you have the score right? Let me make a variable. Let's score be whatever. I'm just going to get an arbitrary value so we can test it out later. Let's give it 85 the number. The score is the variable that is going to hold the score, the number of points. As an example I put 85 right here. We can use this variable in our condition checks. And then please use the variable letter to assign a letter. Since we don't know yet I'm just going to do a letter so you can define later in each of these condition code blocks. The variable score holds the number of points. You have to use this in the conditions. Then once you figure out the conditions you're going to assign a value to the letter variable. So the first condition is what? If the letter grade is a. How can we do this? Well the score has to be between 90 and 100. So let's assume the score never goes beyond 100 so we can just say what? If the score is greater than what? In this case it would be 89 right? So if the score is greater than 89 that is 90, 91 and so on. Assume it never goes beyond 100. We're fine ignoring the upper bounds. And we're going to assign the letter a. Okay let's review what we did. The variable score holds the number of points we want to check. If this is greater than 89 that is it could be 90, it could be 91 all the way to 100. Then we assign the string a to the variable letter. Okay? Now do you know another way of writing this guy? Instead of saying 89 you want to say 90? Yeah another way is you can say greater than or equal to 90. And this is just another way I think it looks better. It's up to you to choose which way. So if the score is greater than or equal to 90 we're going to assign the letter a. Now as an exercise I want you to do the next case which is the letter grade b. Note that this condition will have been false by the time you reach this condition. So pause the video and try it for yourself. So how did it go? So if the score is not greater than or equal to 90 then it has to be less than 90 right? So in order for us to assign the letter b we just need to check if the score is greater than or equal to what? What's the lower bound for b? It's 80. Then if that is the case we assign the letter b. That I did not care for 89 this upper bound because we already knew that it checked already for the value if it's greater than or equal to 90. Since we already know this is false if the score was indeed this condition was less then this would go here and then you just need to check if it's at least 80 because we already know it's at most 89 because of this condition would have failed. Okay and in the same fashion let's do the other one. Now if it's c we expect the score this is going to be false then it's going to be less than 80. So it's less than 80 and we have to check if it's at least 70. So score greater than or equal to 70. In this case we assign the letter c. Notice that I always use the semicolon at the end of my statements as I previously mentioned in another video. That's a convention that I would use. So just so you know. Now let's go to the next condition that's d. Now assuming score is less than 70 it would go to this else if right here then we need only check if the score is at least 60. So if the score is greater than or equal to 60 if that is the case we assign the value d the string containing the character d in uppercase to the variable letter. Okay now for the final case it actually turns out to be pretty easy because we don't have to check any condition. We already know after it went through this through this through this and finally through score greater than or equal to 60 if this turns out to be false it means the score it has to be less than 60 which could be between 0 and 59 assuming the score is never lower than 0. So we were just assigned the letter f for the else case. That's when it fails because all these conditions would have been would have failed if the score in d turned out to be less than 60. So as you can see the if else if construct allows you to check for an infinite number of cases and you just keep checking condition after condition after condition until you hit the last resort which is the else. So in the if else if else construct you're always going to do something no matter what but it's going to check the first one if it's false go to the next one false and go to the next one and so on until it hits the else when everything else fails all the conditions fail. Okay.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: