mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-02-24 20:01:39 -05:00
feat(curriculum): Add interactive examples to Reduce method lesson (#63402)
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user