Higher Order Functions

Trace the Accumulator

Trace the Accumulator

reduce is easier to reason about when you track the accumulator after each callback. The value returned from one callback call becomes the accumulator passed into the next call.

Study this code before editing the answers:

js
const finalTotal = [3, -1, 4].reduce((total, change) => {
  return total + change;
}, 10);

Your task: Trace the accumulator. Assign the accumulator value after the first callback to afterFirst, after the second callback to afterSecond, and the final reduced value to finalTotalPrediction.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

Trace the Accumulator

reduce is easier to reason about when you track the accumulator after each callback. The value returned from one callback call becomes the accumulator passed into the next call.

Study this code before editing the answers:

js
const finalTotal = [3, -1, 4].reduce((total, change) => {
  return total + change;
}, 10);

Your task: Trace the accumulator. Assign the accumulator value after the first callback to afterFirst, after the second callback to afterSecond, and the final reduced value to finalTotalPrediction.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

Console

Run code to see output.