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:
| score | Tests that run | grade |
|---|---|---|
| 85 | >= 90 false, >= 80 true | B |
| 90 | >= 90 true | A |
| 79 | >= 90 false, >= 80 false, >= 70 true | C |
| 59 | all four tests are false, so the else runs | F |
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
- Declare
gradewithletand no value. The branches assign it later, so it can't be aconst. It also has to be declared outside the chain, because a variable declared inside a{ }block disappears when the block ends. - Open the ladder with an
iffor 90 or more that setsgradeto"A". Close its block with}on the line where the next branch continues. - Add an
else iffor 80 or more that sets"B". - Add an
else iffor 70 or more that sets"C". - Add an
else iffor 60 or more that sets"D". - Finish with a plain
elsethat sets"F". It catches everything below 60, sogradealways gets a value. - Print after the chain. The
console.logline 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:
letwithout a value.let grade;creates the variable holdingundefined. If no branch assigned it, the program would printGrade: undefined. The finalelseguarantees that can't happen.- Block scope. A
letdeclared inside a branch exists only in that branch. Movinglet gradeinto the first block would make the last line fail withReferenceError: grade is not defined. constcan't be reassigned. Declaringconst gradewould not work, because assigning to it throwsTypeError: 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 >= 60first gives a D to every score of 60 or more. - Separate
ifstatements. Writing each test as its ownif(noelse) 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 givesGrade: B. - 70 gives
Grade: C, and 69 givesGrade: D. - 60 gives
Grade: D, and 59 givesGrade: F. - 0 gives
Grade: F, and 100 givesGrade: A.
