Problem overview
The method letterGrades receives an array of scores, each an integer from 0 to 100, and returns an array of letters in the same order. The ranges are 90 to 100 for "A", 80 to 89 for "B", 70 to 79 for "C", 60 to 69 for "D" and 0 to 59 for "F".
The work is split in two. The helper letterGrade turns one score into one letter, and letterGrades loops over the array and collects the helper's answers. For [95, 82, 71] the result is ["A", "B", "C"], and for [100, 90, 89] it is ["A", "A", "B"]: 90 is the lowest A, and 89 is one point short.
Approach: one decision, many times
Converting a single score is a decision with five outcomes. Converting an array is that same decision repeated for every item. Writing the decision as its own function keeps each part small: the helper only thinks about one score, and the loop only thinks about walking the array.
Inside the helper, the checks go from the top grade down. A score that is at least 90 returns "A" at once. Any score that gets past the first if is below 90, so the next check only needs "at least 80", and so on. Each test can ignore the ranges above it, because a return already ended the function for those scores.
Step by step
- Check
score >= 90and return"A". - Check
score >= 80and return"B". Here the score is known to be below 90, so this means 80 to 89. - Check
score >= 70and return"C". - Check
score >= 60and return"D". - Return
"F"after the lastif. Only scores below 60 reach this line, so it needs neither a test nor anelse. - Start
gradesas an emptystring[]inletterGrades. The letters are added one at a time. - Loop with
for (const score of scores). It runs once per score, in order. - Push the helper's answer with
grades.push(this.letterGrade(score)). The helper's parameter is also calledscore, but each function has its own variable; the call hands the helper the loop's current value. - Return
gradesafter the loop, so every score is processed first.
The solution in TypeScript
class Solution {
letterGrade(score: number): string {
if (score >= 90) {
return "A";
}
if (score >= 80) {
return "B";
}
if (score >= 70) {
return "C";
}
if (score >= 60) {
return "D";
}
return "F";
}
letterGrades(scores: number[]): string[] {
let grades: string[] = [];
for (const score of scores) {
grades.push(this.letterGrade(score));
}
return grades;
}
}
Language details worth knowing:
this.before the helper's name. Methods of a class call each other throughthis. Without it the compiler reports that it can't find the name, and the call fails at run time.for...ofversusfor...in.for (const score of scores)gives the values.for...inon an array gives the indices as strings, which is not what this loop needs.constin the loop head. Each turn creates a freshscorethat is never reassigned, soconstis correct there.string[]. The annotation saysgradesholds strings. It is removed when the code is compiled to JavaScript, so it checks nothing at run time.- Strings are written with quotes.
"A"is a string; a bareAwould be read as a variable name.
Following the helper by hand
It helps to ask where each score leaves the helper. A score of 95 is stopped by the first test and gets "A". A score of 82 fails the first test, passes the second and gets "B". A score of 40 fails all four tests and reaches the last line, which gives "F". Running the helper through letterGrades on [100, 90, 89] produces ["A", "A", "B"]: 100 and 90 both stop at the first test, while 89 falls through to the second.
Because every score leaves through exactly one return, the helper never returns two letters. The final return "F" also guarantees that every path ends in a value, which a function declared to return string needs. If that last line were missing, a score below 60 would make the function return undefined instead of a letter, and the lesson's compiler settings would not warn about it.
The same pattern works for any set of ranges: put the strictest test first, give each test only its lower limit, and finish with a default.
Edge cases and common mistakes
- Boundaries. 90, 80, 70 and 60 each belong to the higher grade.
letterGrades([80, 79, 70, 69, 60, 59, 0])returns["B", "C", "C", "D", "D", "F", "F"]. Using> 90instead of>= 90would give a 90 a"B". - Checking the lowest range first. If the first test were
score >= 60, a score of 95 would return"D". The order from highest to lowest is what makes the short tests correct. - Returning inside the loop. A
returnthere sends back the first letter only, not the array. - Mixing up the two functions.
letterGradetakes a number and returns a string;letterGradestakes an array and returns an array.
Testing
[95, 82, 71]gives["A", "B", "C"].[65, 40]gives["D", "F"].[100, 90, 89]gives["A", "A", "B"].[80, 79, 70, 69, 60, 59, 0]gives["B", "C", "C", "D", "D", "F", "F"].
