Loading
Lesson 31
Courses / JavaScript for Complete Beginners
Mapping a Score to Letter Grade (If Else If Else Statement Exercise) using JavaScript

Video Transcript

Welcome back to NDK Tech World. In this lesson, we are going to do an exercise to practice the if else statement. The exercise is about mapping a score to a letter grade. So let's get started. Suppose you are in charge of a, you are working for a school and you have to map certain student exam test scores to a letter grade. Let's say as an example, this is the mapping table for the school. For example, if a student does a test and he gets 81 for his score. Now, according to this table, if you get between 80 and 89, you should get a B as your letter grade. So that's what the student would get. Now, suppose there are so many students and doing this by hand is kind of tedious. So you want to write a program to automate all this process. The program would read all the test scores and then it would give you all the letter grades. So how can we accomplish that? We want to just focus on the part of mapping the score to a letter grade. Okay? So it looks like we have some cases here. We have the case where if you get between 90 and 100 points, you're going to get an A. That's the first case. And if you get between 80 and 89, you're going to get a B. If you get between 70 and 79 points, you're going to get a C. If you get between 60 and 69 a D. And if you get any other kind of points, that is below 59, you get an F. You fail. So we have five cases. So how would we accomplish this? Let's go back to the if-else. So we know we can do two cases like this. If-else. And then we learn about if-else-if-else. We can do three cases, but we don't really know how to do five cases. Now, the beauty of if-else-if is that you can add as many as you'd like. So it doesn't have to necessarily be just one else if. So you can actually have multiple. You can have another else if in between. And that would be four conditions. You can add another one and it would be five. So one, two, three, four, five conditions. So this is actually what we would use to solve this problem. So let me just walk through this again so we can review what this does. It starts off at the if condition here. If this condition is true, execute this and ignore everything else. Otherwise, go to the next else if, check for some condition. If the condition is true, execute this block of code and ignore everything else. Now, if this condition turns out to be false, it goes on and checks the next else if condition. Hey, is this true or false? If it's true, execute this, ignore everything else. Skip everything else. Now, if this condition is false, it goes to the next else if, check this condition again. If it's true, execute this, ignores everything else. Otherwise, if this condition is false, you're going to finally end up with the else. And there's nothing else you can do but execute this else block. You can see that's pretty much how it works. Kind of a tedious to walk through, but remember to always walk through if you find yourself confused with what's going on. So this is what we're going to use for this example. It's pretty much that. We just got to figure out the conditions. Every one of these conditions. So we have the condition. The first one is for letter grade A. The second one is for B and C, D and then finally F, which is the case where we don't really care. If all the other conditions have failed, then it has to be an F grade. So let's write it out.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: