Start learning free

LESSON 203 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Report Card V3 in TypeScript: Letter Grade with if / 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: number = 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: number = 85;

let grade: string;
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

    TYPESCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Report Card V3 in TypeScript, explained

    Problem overview

    Version 3 of the Report Card project turns a numeric score into a letter grade. The constant score holds a number, and the program prints Grade: followed by one of five letters:

    ScoreGrade
    90 or moreA
    80 to 89B
    70 to 79C
    60 to 69D
    below 60F

    The boundaries belong to the higher grade: exactly 90 is an A, exactly 80 a B, and 59 is an F. The starting score is 85, so the program prints:

    Grade: B
    

    Approach: a ladder from the top grade down

    The program needs one decision with five outcomes, which is what an if / else if / else chain is for. The conditions are checked from top to bottom, the first true one runs its block, and the rest of the chain is skipped.

    That order is why each test needs only a lower bound. Start with the highest grade: score >= 90. If that is false, the score is already below 90, so the next test can say just score >= 80 and still mean "80 to 89". The same holds for 70 and 60. A final else has no condition at all. It catches everything that is left, which is every score below 60.

    The chain only decides which letter to use, so the letter is stored in a variable that is declared before the chain and printed after it. Printing from inside each branch would also work, but then the console.log line would be repeated five times.

    Tracing the starting score

    With score equal to 85, the first test (85 >= 90) is false, so the chain moves on. The second test (85 >= 80) is true, so grade becomes "B" and the chain ends. The tests for 70 and 60, and the else, never run.

    Why a chain and not separate ifs

    The word else is what links the tests. Consider the same four tests written as separate statements in the order 90, 80, 70, 60, with an else only on the last one. For a score of 95 the first three assign A, B and C in turn, and the last test assigns D, so the program prints Grade: D. In a chain, the first true test stops everything after it.

    Step by step

    1. Declare grade without a value. Use let with the type string and no initial value. A const can't work, because the branches assign to it after it is declared.
    2. Declare it outside the chain. A variable declared inside a block exists only until the block's closing brace, so the later console.log couldn't see it.
    3. Open with the top grade. if (score >= 90) sets grade to "A".
    4. Continue with else if (score >= 80), which sets "B". The else sits on the same line as the previous block's closing brace, so the chain stays one statement.
    5. Add else if (score >= 70) for "C".
    6. Add else if (score >= 60) for "D".
    7. Finish with a plain else for "F", then close the chain with its brace.
    8. Print after the chain. Call console.log with "Grade: " and grade, outside every brace, so it runs exactly once.

    The solution in TypeScript

    const score: number = 85;
    
    let grade: string;
    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);
    

    Details worth knowing:

    • Declaring without a value. let grade: string; creates the variable with no value. Until a branch assigns one, reading it gives undefined. The chain always assigns, because the final else covers every case the earlier tests missed.
    • >= is inclusive. score >= 90 is true for 90 itself, which is why the boundary scores land in the higher grade.
    • String literals. "A" is a string, matching the declared type. The annotation is checked by the compiler and erased from the JavaScript that runs.
    • One branch runs. For a score of 95, the first test is true and the others are never evaluated.

    Edge cases and common mistakes

    • The boundaries. Scores of 90, 80, 70 and 60 print A, B, C and D. A score of 59 prints F. Writing > instead of >= would push every boundary score down one grade.
    • The extremes. 100 prints A and 0 prints F. The chain has no upper limit for A, so a score of 105 also prints A.
    • Checking from the bottom up. A chain that starts with score >= 60 would give every passing score a D, because the first true test wins. Keep the highest threshold first.
    • Separate ifs in place of a chain. Without else, every test is checked on its own, and several can be true for the same score. Each later assignment overwrites the earlier one, so the result depends on the order of the tests. Written from 60 upward, separate ifs print A for 95, but only because the last true test happens to be the right one.
    • Missing final else. If no test matches and nothing assigns grade, the program prints Grade: undefined.
    • Declaring inside a branch. A let grade written inside one of the blocks is not visible after the chain.

    Testing

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