Skip to content

Commit aa9cc2d

Browse files
committed
day 9 higher order function
1 parent 41a3f95 commit aa9cc2d

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

09_Day_Higher_order_functions/09_day_higher_order_functions.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ ICELAND
236236

237237
### map
238238

239-
_map_: Iterate an array elements and modify the array elements. It takes a callback function with elements and index parameter and return a new array.
239+
_map_: Iterate an array elements and modify the array elements. It takes a callback function with elements, index , array parameter and return a new array.
240240

241241
```js
242-
const modifiedArray = arr.map(function (element, index) {
242+
const modifiedArray = arr.map(function (element, index, arr) {
243243
return element
244244
})
245245
```
@@ -327,7 +327,7 @@ console.log(countriesContainingLand)
327327
```
328328

329329
```js
330-
const countriesEndsByia = countries.filter((country) => country.includes('ia'))
330+
const countriesEndsByia = countries.filter((country) => country.endsWith('ia'))
331331
console.log(countriesEndsByia)
332332
```
333333

@@ -365,11 +365,18 @@ console.log(scoresGreaterEight)
365365

366366
### reduce
367367

368-
_reduce_: Reduce takes a callback function. The call back function takes accumulator and current value as a parameter and returns a single value:
368+
_reduce_: Reduce takes a callback function. The call back function takes accumulator, current, and optional initial value as a parameter and returns a single value. It is a good practice to define an initial value for the accumulator value. If we do not specify this parameter, by default accumulator will get array `first value`. If our array is an _empty array_, then `Javascript` will throw an error.
369+
370+
```js
371+
arr.reduce((acc, cur) => {
372+
// some operations goes here before returning a value
373+
return
374+
}, initialValue)
375+
```
369376

370377
```js
371378
const numbers = [1, 2, 3, 4, 5]
372-
const sum = numbers.reduce((accum, curr) => accum + curr)
379+
const sum = numbers.reduce((acc, cur) => acc + cur, 0)
373380

374381
console.log(sum)
375382
```

0 commit comments

Comments
 (0)