Project: summarize a collection of scores
Quick answer
Build a reusable score summary by traversing a dense array of finite numbers. Combining length, iteration and an accumulator avoids relying on later array transformation APIs.
How it works
Define the empty-input policy before averaging. Accumulate total and count passing scores (60 or above) in one loop. Return an array with total, passing count and average; for empty input use zero as the documented average policy.
function summary(scores) { let total = 0; let passed = 0; for (const n of scores) { total += n; if (n >= 60) passed++; } return [total, passed, scores.length ? total / scores.length : 0]; }console.log(JSON.stringify(summary([60, 80, 40])));Expected output in Node.js:
[180,2,60]JSON.stringify here only prints a compact representation of the result array; serialization is covered later.
function average(scores) { let total = 0; for (const n of scores) total += n; return scores.length ? total / scores.length : 0; }console.log(average([]), average([0, 100]));Expected output in Node.js:
0 50The explicit empty policy avoids dividing by zero.
Common mistakes
Do not divide by length without an empty-input decision. Do not initialize every statistic to zero if a minimum or maximum of negative values is required.
Practice
Test a score summary with no values, exactly 60, and a mixture of passing and failing scores.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Project: summarize a collection of scores — Knowledge Check
Apply the taught concept and reason about the result.
For scores [60,80,40], what is the total?
How many scores pass in [59,60,100] with threshold 60 inclusive?
Why explicitly handle an empty scores array before averaging?
What is the average of [20,80]?
Which state is needed to compute total and pass count in one traversal?