Start learning free

LESSON 201 OF 671 · PROJECTS: EVERYDAY PROGRAMS

Report Card V4 in JavaScript: Grades for Many Students

Practice Read the full explanation

Combine lists, loops, and conditionals to print a letter grade for each student in a class.

You start with:

const names = ["Alice", "Bob", "Carol", "Dan"];
const scores = [92, 78, 85, 67];

Build:

Alice: A
Bob: C
Carol: B
Dan: D

Rules:

  • Loop over the indices of the students (names.length) so you can read the name and the score at the same position.
  • Use the same scale as V3: 90+ A, 80-89 B, 70-79 C, 60-69 D, below 60 F.
  • On each iteration print the name, a colon and a space, then the grade, for example Alice: A.

Use a classic for (let i = 0; i < names.length; i++) and apply the grading chain inside.

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);
}

STEP 1 OF 9
    Ctrl + Enter to check

    JAVASCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Report Card V4 in JavaScript, explained

    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

    inames[i]scores[i]grade
    0Alice92A
    1Bob78C
    2Carol85B
    3Dan67D

    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

    1. Write a for loop where i starts at 0 and runs while i < names.length. With four names, i takes the values 0, 1, 2 and 3.
    2. Read the score. Inside the loop, store scores[i] in a const named score. Each pass creates a new score.
    3. Declare let grade inside the loop with no value, to be set by the ladder.
    4. Build the ladder: an if for 90 or more setting "A", then else if branches for 80, 70 and 60 setting "B", "C" and "D".
    5. End with else setting "F", so every score gets a grade.
    6. 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 i in the loop header. The counter must be declared with let, because it changes on every pass. A const counter would throw on the first i++.
    • const score inside the loop. A const is 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. grade is 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] + ": " + grade joins three strings into Alice: 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. If scores were shorter, scores[i] would be undefined past its end, every comparison with undefined would be false, and that student would get an F. For ["Zed", "Yan"] with the scores [75], the output is Zed: C and Yan: 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.length runs one pass too many, reads undefined and prints undefined: F.
    • Printing outside the loop. A console.log placed after the closing brace runs once instead of once per student, and score and grade don'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.length is a reasonable choice, since the output has one line per name.

    Testing

    • The starter data prints Alice: A, Bob: C, Carol: B and Dan: D.
    • Names Eve, Finn, Gus, Hal and Ivy with scores 90, 59, 60, 100 and 0 print Eve: A, Finn: F, Gus: D, Hal: A and Ivy: F.
    • Two empty arrays print nothing.
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free