Problem overview
Given an array of integer scores, return an array with the letter grade of each score, in the same order. A score from 90 to 100 is an "A", 80 to 89 a "B", 70 to 79 a "C", 60 to 69 a "D", and 0 to 59 an "F".
The work is split in two. The helper letterGrade(score) turns one score into one letter. The method letterGrades(scores) goes over the array and collects the helper's letter for each score. For [95, 82, 71] the result is ["A", "B", "C"], and for [100, 90, 89] it is ["A", "A", "B"]: 90 is the lowest score that earns an A, and 89 is one point short.
Approach
There are two separate problems here, and each is easier alone. Grading one score is a chain of range checks. Grading many scores is a loop that repeats one action.
For the chain, test the ranges from the top down. Because return ends the function, a check only runs when every earlier check failed. After the test for 90 and above fails, you know the score is below 90, so "at least 80" really means 80 to 89. Each check therefore needs only one bound instead of two, and the last line, return "F", needs no test at all.
The same chain could be written with else if. The lesson's version, with separate if statements and early returns, behaves identically and reads as a plain list of rules from top to bottom. Splitting the job also means each function can be checked alone: you can call letterGrade on single scores such as 90, 89, 60 and 59 before trusting the loop around it.
Step by step
- Start the helper with the top grade. If
score >= 90, return"A". - Add the B check. Scores of 90 and up have already returned, so
score >= 80means 80 to 89. - Add the C check the same way, with 70.
- Add the D check with 60. This is the last passing range.
- Return
"F"at the end. Only scores below 60 reach this line. - Create
gradesinletterGradesas an empty array. Letters will be added one at a time. - Loop over
scoreswithfor...of. Each score gives exactly one letter, so the loop body runs once per score. - Push the helper's answer.
grades.push(this.letterGrade(score))calls the helper with the current score and appends the result. - Return
gradesafter the loop. Areturninside the loop would stop after the first score.
The solution in JavaScript
class Solution {
letterGrade(score) {
if (score >= 90) {
return "A";
}
if (score >= 80) {
return "B";
}
if (score >= 70) {
return "C";
}
if (score >= 60) {
return "D";
}
return "F";
}
letterGrades(scores) {
let grades = [];
for (let score of scores) {
grades.push(this.letterGrade(score));
}
return grades;
}
}
Details worth knowing:
- Methods call each other with
this.Inside the class,letterGrade(score)on its own is not found and raises aReferenceError.this.letterGrade(score)calls the method on the same object. - Same name, separate variables. The helper's parameter and the loop variable are both called
score. Each function has its own variable, and the call copies the loop's value into the helper's parameter. - Strings are compared by value. The results are plain strings, and
"A"and"A"are equal wherever they came from. - Quotes and case.
"A"and'A'are the same string in JavaScript, so either quote style works. The letter itself must be uppercase:"a"is a different string and fails the lesson's tests. pushchanges the array in place. It is whygradescan be declared withletand still collect values: the array itself changes, and the variable keeps pointing to it.
Edge cases and common mistakes
- The boundaries.
letterGrade(90)is"A",letterGrade(89)is"B",letterGrade(60)is"D"andletterGrade(59)is"F". Using>instead of>=would move every boundary score down one grade. - Checking the lowest range first. If the first test were
score >= 60, a 95 would get a"D", because that test also passes for it. Start at the top. - Zero. A score of 0 falls through every check and gets
"F", which is correct. - Keeping the order. The result must line up with the input, so push inside the loop in input order. Don't sort the scores.
- Returning inside the loop. It returns a single letter or a one-element array instead of the full list.
Why a helper
The loop in letterGrades knows nothing about grade ranges, and the helper knows nothing about arrays. If the grading scale changed, only letterGrade would need an edit. If the input became a different kind of collection, only the loop would. This separation is the habit the lesson practises.
Testing
scores = [95, 82, 71]gives["A", "B", "C"].scores = [65, 40]gives["D", "F"].scores = [100, 90, 89]gives["A", "A", "B"].letterGrade(0)gives"F", andletterGrade(60)gives"D".
