feat(curriculum): Add interactive examples to Reduce method lesson (#63402)

This commit is contained in:
Giftea ☕
2025-10-31 21:29:07 +01:00
committed by GitHub
parent 86c334b81f
commit 0ef0992514

View File

@@ -5,7 +5,7 @@ challengeType: 19
dashedName: what-is-the-reduce-method-and-how-does-it-work
---
# --description--
# --interactive--
The `reduce` method is a function in JavaScript that allows you to process an array and condense it into a single value. This single value can be a number, a string, an object, or even another array.
@@ -17,6 +17,8 @@ The reducer function takes two main parameters: an accumulator and the current v
Let's look at an example to illustrate how `reduce` works:
:::interactive_editor
```js
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce(
@@ -27,6 +29,8 @@ const sum = numbers.reduce(
console.log(sum); // 15
```
:::
In this example, we're using `reduce` to get the sum of all the numbers in the array.
The reducer function takes the accumulator (which starts at `0`, as specified by the second argument to `reduce`) and adds each number to it.