Update 09_day_higher_order_functions.md

Edit  map definition, add  some code example for reduce.
pull/91/head
Chety 5 years ago committed by GitHub
parent f231757a79
commit a76a7edd4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -235,10 +235,10 @@ ICELAND
### map ### map
_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. _map_: Iterate an array elements and modify the array elements. It takes a callback function with element,index,array parameter and return a new array.
```js ```js
const modifiedArray = arr.map(function(element, index) { const modifiedArray = arr.map(function(element, index, arr) {
return element return element
}) })
``` ```
@ -326,7 +326,7 @@ console.log(countriesContainingLand)
``` ```
```js ```js
const countriesEndsByia = countries.filter(country => country.includes('ia')) const countriesEndsByia = countries.filter(country => country.endsWith('ia'))
console.log(countriesEndsByia) console.log(countriesEndsByia)
``` ```
@ -373,9 +373,17 @@ const sum = numbers.reduce((accum, curr) => accum + curr)
console.log(sum) console.log(sum)
``` ```
```js `15`
15 > It is a good practice to define an initial value for the accumulator value.
**Reduce** also has a second _optional_ parameter for _accumulator default 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.
```javascript
const numbers = [2,3,4,5,6,7,8,9];
const multiply = arr => arr.reduce((acc,curr) => acc * curr, 1);
``` ```
`multiply(numbers); // -> 362880`
### every ### every

Loading…
Cancel
Save