Start learning free

LESSON 203 OF 671 · PROJECTS: EVERYDAY PROGRAMS

Report Card V6 in JavaScript: Class Statistics

Practice Read the full explanation

Compute three class statistics from a list of scores: the highest, the lowest, and the average.

You start with:

const scores = [92, 78, 85, 67, 74, 88, 91, 58];

Build:

Highest: 92
Lowest: 58
Average: 79

Rules:

  • highest is the largest value in scores.
  • lowest is the smallest value in scores.
  • average is the sum of all scores divided by how many there are, rounded down (Math.floor(total / scores.length)).
  • Print "Highest: ", "Lowest: " and "Average: " on three lines, in the order of the sample output (console.log("Highest: " + highest);).

Math.max(...scores) spreads the array into Math.max's arguments. Add the scores up in a loop, then use Math.floor for the integer average.

const scores = [92, 78, 85, 67, 74, 88, 91, 58];

const highest = Math.max(...scores);
const lowest = Math.min(...scores);
let total = 0;
for (const s of scores) total += s;
const average = Math.floor(total / scores.length);

console.log("Highest: " + highest);
console.log("Lowest: " + lowest);
console.log("Average: " + average);

STEP 1 OF 8
    Ctrl + Enter to check

    JAVASCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Report Card V6 in JavaScript, explained

    Problem overview

    The last report card version summarises a whole class. Given an array of scores, the program prints the highest score, the lowest score and the average, rounded down:

    Highest: 92
    Lowest: 58
    Average: 79
    

    The starter array is [92, 78, 85, 67, 74, 88, 91, 58]. The biggest value is 92 and the smallest is 58. The scores add up to 633, and 633 divided by 8 is 79.125, which rounds down to 79.

    Approach: built-ins for the extremes, a loop for the sum

    Finding the largest and smallest values could be done with a loop that remembers the best so far, but JavaScript has Math.max and Math.min, which take any number of arguments and return the largest or smallest. They take separate numbers, not an array, so the array is spread into arguments with the three dots: Math.max(...scores) is the same as writing Math.max(92, 78, 85, 67, 74, 88, 91, 58).

    The sum has no single built-in call in this lesson, so a loop adds each score to a running total that starts at 0. The average is the total divided by the number of scores, scores.length, and Math.floor drops the part after the decimal point.

    Step by step

    1. highest. A const holding Math.max(...scores). One call is enough, with no loop.
    2. lowest. The mirror image: Math.min(...scores).
    3. total. Create it with let and start at 0. It changes as scores are added, so it can't be a const.
    4. Add every score. A for (const s of scores) loop takes each score in turn, 92, then 78 and so on. The loop has a single statement, total += s, so it fits on one line without braces.
    5. average. Divide total by scores.length and wrap the division in Math.floor. The plain division gives 79.125 here.
    6. Print three lines with console.log, in the order Highest, Lowest, Average. Each is the label followed by the value.

    The solution in JavaScript

    const scores = [92, 78, 85, 67, 74, 88, 91, 58];
    
    const highest = Math.max(...scores);
    const lowest = Math.min(...scores);
    let total = 0;
    for (const s of scores) total += s;
    const average = Math.floor(total / scores.length);
    
    console.log("Highest: " + highest);
    console.log("Lowest: " + lowest);
    console.log("Average: " + average);
    

    Language details worth knowing:

    • Spread. ...scores unpacks the array's elements into separate arguments. Without it, Math.max(scores) receives one array. Math.max([5]) happens to return 5, because a one-element array converts to a number, but Math.max([1, 2]) returns NaN, so the spread is required.
    • total += s is shorthand for total = total + s. It is a loop body without braces, which is allowed for a single statement. Braces are still a good habit once a loop grows.
    • Division gives decimals. JavaScript's / returns 79.125 for 633 / 8, not 79. Math.floor rounds down to the nearest whole number.
    • Reading scores.length. It is the count of elements, so the program keeps working if scores are added to or removed from the array.

    Edge cases and common mistakes

    • Forgetting the spread. Math.max(scores) with the whole eight-element array returns NaN, and the program prints Highest: NaN.
    • Starting total at the wrong value. It has to be 0. If it starts as undefined (by declaring let total;), adding to it produces NaN.
    • Dividing by a hard-coded 8. It works for this data but breaks as soon as the array changes size. Use scores.length.
    • One score. With [80] the highest, lowest and average are all 80.
    • Rounding. Math.floor rounds down, so [70, 71] has an average of 70.5 and prints Average: 70.
    • An empty array. Math.max() with no arguments is -Infinity and Math.min() is Infinity, and the total of nothing is 0 divided by 0, which is NaN. The lesson's tests don't cover this case, but real code would check scores.length first.

    Testing

    • The starter array prints Highest: 92, Lowest: 58 and Average: 79.
    • [80] prints 80 on all three lines.
    • [70, 71] prints Highest: 71, Lowest: 70 and Average: 70.
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free