Problem overview
Version 4 grades a whole class. The starter code has two parallel arrays: names holds four student names and scores holds their scores, so scores[i] belongs to names[i]. The program applies version 3's grading scale (90 and up A, 80s B, 70s C, 60s D, below 60 F) to every student and prints one line each:
Alice: A
Bob: C
Carol: B
Dan: D
Approach: one index, two arrays
Version 3 graded one score. To grade several, the same ladder runs inside a loop. The loop counts positions rather than visiting the names directly, because the position is the link between the two arrays. When i is 2, names[2] is "Carol" and scores[2] is 85, so the line for Carol is built from both.
The ladder itself is unchanged, so this version is about moving it into a loop body. Everything that belongs to one student (the score, the grade and the printing) goes inside the loop, so each pass starts fresh.
What each pass does
| i | names[i] | scores[i] | grade |
|---|---|---|---|
| 0 | Alice | 92 | A |
| 1 | Bob | 78 | C |
| 2 | Carol | 85 | B |
| 3 | Dan | 67 | D |
After the pass with i = 3, i++ makes i equal to 4, the condition i < names.length is false, and the loop ends. Each row of the table is one run of the loop body, and the row becomes one printed line.
Step by step
- Write a
forloop whereistarts at 0 and runs whilei < names.length. With four names,itakes the values 0, 1, 2 and 3. - Read the score. Inside the loop, store
scores[i]in aconstnamedscore. Each pass creates a newscore. - Declare
let gradeinside the loop with no value, to be set by the ladder. - Build the ladder: an
iffor 90 or more setting"A", thenelse ifbranches for 80, 70 and 60 setting"B","C"and"D". - End with
elsesetting"F", so every score gets a grade. - Print inside the loop, after the ladder: the name, a colon and a space, then the grade. Because it is inside the loop, it runs once per student and produces four lines.
The solution in JavaScript
const names = ["Alice", "Bob", "Carol", "Dan"];
const scores = [92, 78, 85, 67];
for (let i = 0; i < names.length; i++) {
const score = scores[i];
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(names[i] + ": " + grade);
}
The details to notice:
let iin the loop header. The counter must be declared withlet, because it changes on every pass. Aconstcounter would throw on the firsti++.const scoreinside the loop. Aconstis fine here. Each pass of the loop body is a new block, so a new constant is created every time instead of being reassigned.- Everything per student lives inside the loop.
gradeis declared in the body, so one student's grade can't leak into the next. If it were declared outside, a missing assignment would silently keep the previous student's grade. - String joining.
names[i] + ": " + gradejoins three strings intoAlice: A.
Why an index loop
A for...of loop over names would hand you each name but not its position, so the matching score would be out of reach. The two arrays are linked only by position, which makes the classic counting loop the natural tool here.
The pattern here is the one you will use whenever two lists describe the same items: pick one list to drive the loop, and read every other list at the same index. It only works while the lists stay in the same order, so a name and its score must always sit at the same position. Sorting one array without the other would silently pair students with the wrong scores.
Edge cases and common mistakes
- Two arrays of different lengths. The loop is driven by
names.length. Ifscoreswere shorter,scores[i]would beundefinedpast its end, every comparison withundefinedwould be false, and that student would get an F. For["Zed", "Yan"]with the scores[75], the output isZed: CandYan: F. - An empty class. With two empty arrays the loop body never runs and nothing is printed.
- Boundaries. Scores 90, 60 and 59 give A, D and F, and 100 and 0 give A and F. With names Eve, Finn, Gus, Hal and Ivy and the scores 90, 59, 60, 100 and 0, the output is A, F, D, A and F in that order.
- Off-by-one in the loop condition.
i <= names.lengthruns one pass too many, readsundefinedand printsundefined: F. - Printing outside the loop. A
console.logplaced after the closing brace runs once instead of once per student, andscoreandgradedon't even exist there, because they were declared inside the loop's block. - Looping over the wrong array. Both arrays should have the same length. Driving the loop from
names.lengthis a reasonable choice, since the output has one line per name.
Testing
- The starter data prints
Alice: A,Bob: C,Carol: BandDan: D. - Names Eve, Finn, Gus, Hal and Ivy with scores 90, 59, 60, 100 and 0 print
Eve: A,Finn: F,Gus: D,Hal: AandIvy: F. - Two empty arrays print nothing.
