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
highest. AconstholdingMath.max(...scores). One call is enough, with no loop.lowest. The mirror image:Math.min(...scores).total. Create it withletand start at 0. It changes as scores are added, so it can't be aconst.- 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. average. Dividetotalbyscores.lengthand wrap the division inMath.floor. The plain division gives 79.125 here.- Print three lines with
console.log, in the orderHighest,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.
...scoresunpacks 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, butMath.max([1, 2])returnsNaN, so the spread is required. total += sis shorthand fortotal = 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 for633 / 8, not 79.Math.floorrounds 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 returnsNaN, and the program printsHighest: NaN. - Starting
totalat the wrong value. It has to be 0. If it starts asundefined(by declaringlet total;), adding to it producesNaN. - 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.floorrounds down, so[70, 71]has an average of 70.5 and printsAverage: 70. - An empty array.
Math.max()with no arguments is-InfinityandMath.min()isInfinity, and the total of nothing is 0 divided by 0, which isNaN. The lesson's tests don't cover this case, but real code would checkscores.lengthfirst.
Testing
- The starter array prints
Highest: 92,Lowest: 58andAverage: 79. [80]prints 80 on all three lines.[70, 71]printsHighest: 71,Lowest: 70andAverage: 70.
