QUESTION 11 / 20 · ARRAYS · BEGINNER

Carry a running total

What will this code print? Read each line, then make your prediction.

Where this matters

A running total is useful for adding quantities in a cart. The initial accumulator determines where the calculation starts.

JavaScript · read-only example
console.log([2, 3, 5].reduce((sum, value) => sum + value, 0));
Your prediction

Read the full explanation

The accumulator starts at zero, becomes two, then five, then ten. Each returned sum is passed to the next callback.

Trace the code

  1. The explicit initial accumulator is 0.
  2. The callback returns 2, then 5, then 10 as each input value is added.
  3. Reduce returns the final accumulator, not a list of intermediate totals.

Why the other predictions differ

  • 5 — Five is an intermediate accumulator, not the final one.
  • 235 — The operands are numbers, so + adds instead of concatenating strings.
Expected console output
10

Reading this explanation does not record mastery. A correct submitted attempt can do so, including after review.

Change one thing

Start the accumulator at 10, keeping the same array.

Predict the result, then run this self-contained snippet in your browser’s developer console. It uses only sample values and makes no network requests.

console.log([2, 3, 5].reduce((sum, value) => sum + value, 10));
Check the variation’s output
20

Explain which binding, value, or operation changed before moving to the next challenge.