Start learning free

LESSON 200 OF 671 · PROJECTS: EVERYDAY PROGRAMS

Report Card V3 in JavaScript: Letter Grade with else if

Practice Read the full explanation

Use if/else if/else to turn a numeric score into a letter grade.

You start with:

const score = 85;

Build:

Grade: B

Rules:

  • score >= 90 is "A"; 80-89 is "B"; 70-79 is "C"; 60-69 is "D"; below 60 is "F".
  • The boundaries count: a score of exactly 90 is an A, exactly 80 a B, exactly 70 a C, exactly 60 a D, and 59 is an F.
  • Print "Grade: " followed by the letter grade, for example console.log("Grade: " + grade);

Declare let grade; first, then assign in an if/else if chain.

const score = 85;

let grade;
if (score >= 90) {
    grade = "A";
} else if (score >= 80) {
    grade = "B";
} else if (score >= 70) {
    grade = "C";
} else if (score >= 60) {
    grade = "D";
} else {
    grade = "F";
}
console.log("Grade: " + grade);

STEP 1 OF 7
    Ctrl + Enter to check

    JAVASCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Report Card V3 in JavaScript, explained

    Problem overview

    Version 3 turns a numeric score into a letter. The scale is: 90 and above is A, 80 to 89 is B, 70 to 79 is C, 60 to 69 is D, and anything below 60 is F. The boundaries belong to the higher grade, so exactly 90 is an A and 59 is an F.

    The starter code has const score = 85;, so the program prints Grade: B.

    Approach: a ladder of conditions, highest first

    An if / else if / else chain tests its conditions from the top and runs the first block whose condition is true. All the blocks after it are skipped. That lets each test state only a lower limit.

    Check the highest grade first. If score >= 90 is false, the score is already below 90, so the next test score >= 80 really means "80 to 89". The same reasoning holds all the way down, and the final else needs no condition because only scores below 60 reach it.

    The order matters. A ladder that starts with score >= 60 would give a D to a score of 95, because that first test is true for it and the rest never run.

    Tracing a score through the ladder

    Here is what happens for a few scores, from the first test that is true:

    scoreTests that rungrade
    85>= 90 false, >= 80 trueB
    90>= 90 trueA
    79>= 90 false, >= 80 false, >= 70 trueC
    59all four tests are false, so the else runsF

    A score never runs more tests than it needs: the chain stops at the first true condition. That is the difference between an else if chain and several independent if statements, which would all be tested every time.

    Step by step

    1. Declare grade with let and no value. The branches assign it later, so it can't be a const. It also has to be declared outside the chain, because a variable declared inside a { } block disappears when the block ends.
    2. Open the ladder with an if for 90 or more that sets grade to "A". Close its block with } on the line where the next branch continues.
    3. Add an else if for 80 or more that sets "B".
    4. Add an else if for 70 or more that sets "C".
    5. Add an else if for 60 or more that sets "D".
    6. Finish with a plain else that sets "F". It catches everything below 60, so grade always gets a value.
    7. Print after the chain. The console.log line sits outside the braces, so it runs once, after a grade has been chosen.

    The solution in JavaScript

    const score = 85;
    
    let grade;
    if (score >= 90) {
        grade = "A";
    } else if (score >= 80) {
        grade = "B";
    } else if (score >= 70) {
        grade = "C";
    } else if (score >= 60) {
        grade = "D";
    } else {
        grade = "F";
    }
    console.log("Grade: " + grade);
    

    Language details worth knowing:

    • let without a value. let grade; creates the variable holding undefined. If no branch assigned it, the program would print Grade: undefined. The final else guarantees that can't happen.
    • Block scope. A let declared inside a branch exists only in that branch. Moving let grade into the first block would make the last line fail with ReferenceError: grade is not defined.
    • const can't be reassigned. Declaring const grade would not work, because assigning to it throws TypeError: Assignment to constant variable.
    • Comparison operators. >= includes the boundary value. Using > would turn exactly 90 into a B.

    Edge cases and common mistakes

    • Boundaries. Scores 90, 80, 70 and 60 give A, B, C and D, and 89, 79, 69 and 59 give B, C, D and F. These are the values most worth testing.
    • Wrong order. A ladder that tests score >= 60 first gives a D to every score of 60 or more.
    • Separate if statements. Writing each test as its own if (no else) makes every true test run, so the last one wins and a 95 would end as a D.
    • Scores outside 0 to 100. The ladder doesn't check the range: 100 gives A, and 0 or a negative score gives F.
    • Decimals. A score such as 89.5 also works, since it is at least 80 but below 90: the result is B.

    Why a ladder and not a formula

    You might notice that the grade depends on the tens digit of the score, and wonder whether arithmetic could replace the chain. It could for A to D, but the scale has a special case at the bottom (everything below 60 is an F, not several different letters), and the rules might change. Moving the B boundary to 83 means editing one number in a ladder, while a formula would need rethinking. Written as conditions, the code reads like the rule it implements, which is why this lesson uses it, and it is easy to extend later.

    Testing

    • 85 gives Grade: B.
    • 90 gives Grade: A, and 89 gives Grade: B.
    • 70 gives Grade: C, and 69 gives Grade: D.
    • 60 gives Grade: D, and 59 gives Grade: F.
    • 0 gives Grade: F, and 100 gives Grade: A.
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free