Start learning free

LESSON 204 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Report Card V4 in Scala: Grades for Multiple 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:

val names = Array("Alice", "Bob", "Carol", "Dan")
val scores = Array(92, 78, 85, 67)

Build:

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

Rules:

  • Loop over the indices of the students (names.indices) 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 Scala arrays and for (i <- 0 until names.size).

val names = Array("Alice", "Bob", "Carol", "Dan")
val scores = Array(92, 78, 85, 67)

for (i <- 0 until names.size) {
    val score = scores(i)
    var 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"
    }
    println(names(i) + ": " + grade)
}

STEP 1 OF 9
    Ctrl + Enter to check

    SCALA · PROJECTS: EVERYDAY PROGRAMS

    Report Card V4 in Scala, explained

    Problem overview

    Version 4 grades a whole class. The starter code has two parallel arrays: names holds "Alice", "Bob", "Carol" and "Dan", and scores holds 92, 78, 85 and 67. The student at position i in one array is the student at position i in the other.

    The program prints one line per student, with the name, a colon, a space and the letter grade. The scale is the one from the previous version: 90 and above is A, 80 to 89 B, 70 to 79 C, 60 to 69 D and anything lower F. The expected output is:

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

    Approach

    The grading ladder from version 3 stays the same. What changes is that it must run once for each student, so it goes inside a loop. The loop variable i visits the positions 0, 1, 2 and 3, and each pass reads scores(i) for the ladder and names(i) for the output.

    Parallel arrays only work while both have the same length and the same order. The loop trusts that, and it makes the whole program short: no student objects, no pairs, only an index shared by two arrays.

    Step by step

    1. Write the loop. for (i <- 0 until names.size) makes i take the values from 0 up to, but not including, the number of names. Close its brace at once; everything else goes inside.
    2. Read the score. val score = scores(i). Scala indexes an array with parentheses, not square brackets.
    3. Declare grade. A var that starts as empty text, because the ladder assigns it afterwards. Declaring it inside the loop gives every student a fresh one.
    4. Start the ladder with an if for 90 or more that sets "A".
    5. Add else if for 80 or more, which sets "B". The score is already known to be below 90.
    6. Add else if for 70 or more, which sets "C".
    7. Add else if for 60 or more, which sets "D".
    8. End with else, which sets "F", and close the ladder.
    9. Print the student. Still inside the loop, after the ladder, so the line appears once per student.

    The solution in Scala

    val names = Array("Alice", "Bob", "Carol", "Dan")
    val scores = Array(92, 78, 85, 67)
    
    for (i <- 0 until names.size) {
        val score = scores(i)
        var 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"
        }
        println(names(i) + ": " + grade)
    }
    

    What each pass does

    The loop runs four times. Each pass reads one position from both arrays, runs the ladder and prints one line:

    inames(i)scores(i)printed
    0Alice92Alice: A
    1Bob78Bob: C
    2Carol85Carol: B
    3Dan67Dan: D

    Because grade is created inside the loop body, nothing carries over from one student to the next. Each pass begins with an empty string and ends with a letter.

    Details that matter in Scala

    0 until names.size is a Range that excludes its upper end, so for four names it covers 0 to 3. The solution could also write names.indices, which is the same range: printing either one gives Range 0 until 3 for a three-element array. A closed range, 0 to names.size, would include the last number and go one step too far.

    names and scores are arrays. An Array is mutable but has a fixed size, and names(i) reads the element at position i. The line names(i) + ": " + grade joins strings with +, which is fine here because both sides are text.

    A for loop without yield is shorthand for calling foreach on the range. The Range object stores only its start, end and step, so a range over a million positions takes no more memory than one over four. The loop body is ordinary code, so it can declare val and var names that live only for that pass.

    Edge cases and common mistakes

    • Going one step too far. With for (i <- 0 to names.size) on an array of two names, the loop prints both names and then fails reading position 2 with Index 2 out of bounds for length 2.
    • Arrays of different lengths. If names has two entries and scores has one, the first line prints (Hal: C for a score of 75) and the second pass fails with Index 1 out of bounds for length 1. The loop assumes the arrays match.
    • Empty arrays. With no students the loop body never runs and nothing is printed.
    • Declaring grade outside the loop. It would still work because every pass assigns it, but each student's grade should belong to that student's pass.
    • Boundary scores. A student with 90 gets an A, one with 59 gets an F and one with 60 gets a D. Running the ladder on the scores 90, 59 and 60 for Eve, Finn and Gus prints Eve: A, Finn: F and Gus: D.

    Testing

    Try the loop on other classes before trusting it. Scores of 90, 59 and 60 give an A, an F and a D, which checks the boundaries. An empty pair of arrays prints nothing. A class of one student prints one line. If a result looks wrong, print i and score inside the loop and compare them with the table above.

    Key takeaways

    • A shared index reads parallel arrays: names(i) goes with scores(i).
    • 0 until n stops before n; 0 to n includes it.
    • Put a per-item calculation, and its output, inside the loop.
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free