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:
| Score | Grade |
|---|---|
| 90 or more | A |
| 80 to 89 | B |
| 70 to 79 | C |
| 60 to 69 | D |
| below 60 | F |
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
- Declare
gradewithout a value. Useletwith the typestringand no initial value. Aconstcan't work, because the branches assign to it after it is declared. - Declare it outside the chain. A variable declared inside a block exists only until the block's closing brace, so the later
console.logcouldn't see it. - Open with the top grade.
if (score >= 90)setsgradeto"A". - Continue with
else if (score >= 80), which sets"B". Theelsesits on the same line as the previous block's closing brace, so the chain stays one statement. - Add
else if (score >= 70)for"C". - Add
else if (score >= 60)for"D". - Finish with a plain
elsefor"F", then close the chain with its brace. - Print after the chain. Call
console.logwith"Grade: "andgrade, 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 givesundefined. The chain always assigns, because the finalelsecovers every case the earlier tests missed. >=is inclusive.score >= 90is 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 >= 60would give every passing score a D, because the first true test wins. Keep the highest threshold first. - Separate
ifs in place of a chain. Withoutelse, 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, separateifs 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 assignsgrade, the program printsGrade: undefined. - Declaring inside a branch. A
let gradewritten inside one of the blocks is not visible after the chain.
Testing
- Score 85 prints
Grade: B. - Score 90 prints
Grade: A, and 100 printsGrade: A. - Score 80 prints
Grade: B. - Score 70 prints
Grade: C. - Score 60 prints
Grade: D, and 59 printsGrade: F. - Score 0 prints
Grade: F.
