Re-arranged code examples

pull/722/head
Mansi 3 years ago committed by GitHub
parent 7fc6e874ee
commit 0636951abe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -89,6 +89,7 @@ Let us see were we use call back functions. For instance the _forEach_ method us
```js ```js
const numbers = [1, 2, 3, 4, 5] const numbers = [1, 2, 3, 4, 5]
const sumArray = arr => { const sumArray = arr => {
let sum = 0 let sum = 0
const callback = function(element) { const callback = function(element) {
@ -96,8 +97,8 @@ const sumArray = arr => {
} }
arr.forEach(callback) arr.forEach(callback)
return sum return sum
} }
console.log(sumArray(numbers)) console.log(sumArray(numbers))
``` ```
@ -116,8 +117,8 @@ const sumArray = arr => {
sum += element sum += element
}) })
return sum return sum
} }
console.log(sumArray(numbers)) console.log(sumArray(numbers))
``` ```
@ -191,10 +192,8 @@ arr.forEach((element, index, arr) => console.log(index, element, arr))
``` ```
```js ```js
let sum = 0;
const numbers = [1, 2, 3, 4, 5]; const numbers = [1, 2, 3, 4, 5];
numbers.forEach(num => console.log(num)) numbers.forEach(num => console.log(num))
console.log(sum)
``` ```
```sh ```sh
@ -238,12 +237,13 @@ _map_: Iterate an array elements and modify the array elements. It takes a callb
const modifiedArray = arr.map(function (element, index, arr) { const modifiedArray = arr.map(function (element, index, arr) {
return element return element
}) })
```
```js
/*Arrow function and explicit return /*Arrow function and explicit return
const modifiedArray = arr.map((element,index) => element); const modifiedArray = arr.map((element,index, arr) => element);
*/ */
```
```js
//Example //Example
const numbers = [1, 2, 3, 4, 5] const numbers = [1, 2, 3, 4, 5]
const numbersSquare = numbers.map((num) => num * num) const numbersSquare = numbers.map((num) => num * num)
@ -471,15 +471,16 @@ console.log(age) // 5
_some_: Check if some of the elements are similar in one aspect. It returns boolean _some_: Check if some of the elements are similar in one aspect. It returns boolean
```js ```js
const names = ['Asabeneh', 'Mathias', 'Elias', 'Brook']
const bools = [true, true, true, true] const bools = [true, true, true, true]
const areSomeTrue = bools.some((b) => b === true) const areSomeTrue = bools.some((b) => b === true)
console.log(areSomeTrue) //true console.log(areSomeTrue) //true
``` ```
```js ```js
const names = ['Asabeneh', 'Mathias', 'Elias', 'Brook']
const areAllStr = names.some((name) => typeof name === 'number') // Are all strings ? const areAllStr = names.some((name) => typeof name === 'number') // Are all strings ?
console.log(areAllStr) // false console.log(areAllStr) // false
``` ```

Loading…
Cancel
Save