BeginnerCoder

Lesson 5 — Processing data

Fold a list into one number

Servers constantly turn lists into totals. reduce walks an array and carries an accumulator (a running result) from item to item. You give it a function (sum, item) => newSum and a starting value.

[1, 2, 3].reduce((s, n) => s + n, 0);   // 6

Why it matters: cart totals, invoice sums and stats are all reduce.

Want it another way?
Task

Use reduce to add up the prices in the cart, so it logs total: 35.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.