An accumulator is the running result in a reduction. Its function receives the current accumulator and the current array element, then returns the next accumulator value. section 12.11
function reduce(array, init, f) { #1
var accum = init; #2
forEach(array, function(element) {
accum = f(accum, element); #3
});
return accum; #4
}
Follow the reduction loop: initialize the accumulator, update it for each element, and return the final value. section 12.11
Counting purchases
A purchase counter starts at 0. For each customer, the accumulator function adds that customer’s purchase count: reduce(customers, 0, function(total, customer) { return total + customer.purchase.length; }). section 12.11
Choose the initial value to match the operation:
- Use 0 for addition and an empty string for string concatenation. section 12.12
- Use 1 for multiplication; reduction over an empty array returns the initial value. section 12.12



