
A checkout view needs the number of items across several lines, including when a customer removes the last line. The initial zero makes the reduction's empty-input result explicit. It also keeps the accumulator numeric from the first row.
A runnable JavaScript fixture
The example keeps the input to Array.reduce outside the function, so it is possible to run the same transformation in a test. The comparison uses concrete data rather than the rendered component. If the application accepts a value from JSON, run the fixture after parsing that JSON to preserve the relevant types.
function evaluate(value) {
return value.reduce((n,item)=>n+item.qty,0);
}
const samples = [[{"qty": 2}, {"qty": 3}], [{"qty": 0}], []];
for (const sample of samples) console.log(evaluate(sample));The result for three concrete cases
| Input | Return value |
|---|---|
| [{"qty": 2}, {"qty": 3}] | 5 |
| [{"qty": 0}] | 0 |
| [] | 0 |
For [{"qty": 2}, {"qty": 3}], the observed output is 5. With [{"qty": 0}], it becomes 0. The useful comparison is at the application boundary. Run the pair against the input as it reaches the program after decoding or parsing, then inspect what the next component receives.
The value for [] is 0. Record the unit or indexing convention next to that expectation. A second implementation can return the same type and still disagree about where a boundary falls or which observations enter the calculation.
An acceptance check for the real feature
A string quantity can switch addition into concatenation. Parse and validate input at the boundary, not midway through aggregation.
Run one case twice through the real feature and look at both the displayed value and its stored representation. A component may coerce a result to text even when the implementation returns a numeric measurement. Keep a failing sample small enough that it can travel with a bug report, including the browser or Node version that produced it.
One more fixture before integrating
Keep item identity with quantity in a real order-line input. The same total count can result from a missing line and a duplicated line, so a count assertion alone is inadequate for higher-value reconciliation.
The same JavaScript function was also run with [] / [{"qty": 2}] / [{"qty": 3}, {"qty": 2}]. These additional inputs change the boundary without changing the implementation. An exception name in the table records where the operation rejects an input; a returned empty or zero value records a successful call. Choose deliberately which of those outcomes the surrounding feature should expose.
| Additional fixture | Result or exception |
|---|---|
| [] | 0 |
| [{"qty": 2}] | 2 |
| [{"qty": 3}, {"qty": 2}] | 5 |
JavaScript standard library reference · MDN JavaScript language guide
Sources and further reading
Cover photograph: Wikimedia Morocco Mini-Hackathon 2026 day 2 - Person seated at desk with laptop and lanyard - 0177 by DERDAKI ILIAS. CC BY-SA 4.0. Cropped and typeset for this cover. The photograph illustrates the topic. This image adaptation uses the same share-alike license.
