Merge pull request #70 from piyuesh-dev/correct-array-method-names
Correct array method names
This commit is contained in:
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Use recursion.
|
Use recursion.
|
||||||
For each letter in the given string, create all the partial anagrams for the rest of its letters.
|
For each letter in the given string, create all the partial anagrams for the rest of its letters.
|
||||||
Use `map()` to combine the letter with each partial anagram, then `reduce()` to combine all anagrams in one array.
|
Use `Array.map()` to combine the letter with each partial anagram, then `Array.reduce()` to combine all anagrams in one array.
|
||||||
Base cases are for string `length` equal to `2` or `1`.
|
Base cases are for string `length` equal to `2` or `1`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
### Average of array of numbers
|
### Average of array of numbers
|
||||||
|
|
||||||
Use `reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.
|
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const average = arr =>
|
const average = arr =>
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
### Count occurrences of a value in array
|
### Count occurrences of a value in array
|
||||||
|
|
||||||
Use `reduce()` to increment a counter each time you encounter the specific value inside the array.
|
Use `Array.reduce()` to increment a counter each time you encounter the specific value inside the array.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0);
|
const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0);
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
### Deep flatten array
|
### Deep flatten array
|
||||||
|
|
||||||
Use recursion.
|
Use recursion.
|
||||||
Use `reduce()` to get all elements that are not arrays, flatten each element that is an array.
|
Use `Array.reduce()` to get all elements that are not arrays, flatten each element that is an array.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const deepFlatten = arr =>
|
const deepFlatten = arr =>
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
### Flatten array
|
### Flatten array
|
||||||
|
|
||||||
Use `reduce()` to get all elements inside the array and `concat()` to flatten them.
|
Use `Array.reduce()` to get all elements inside the array and `concat()` to flatten them.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const flatten = arr => arr.reduce( (a, v) => a.concat(v), []);
|
const flatten = arr => arr.reduce( (a, v) => a.concat(v), []);
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
### Initialize array with range
|
### Initialize array with range
|
||||||
|
|
||||||
Use `Array(end-start)` to create an array of the desired length, `map()` to fill with the desired values in a range.
|
Use `Array(end-start)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range.
|
||||||
You can omit `start` to use a default value of `0`.
|
You can omit `start` to use a default value of `0`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
### Powerset
|
### Powerset
|
||||||
|
|
||||||
Use `reduce()` combined with `map()` to iterate over elements and combine into an array containing all combinations.
|
Use `Array.reduce()` combined with `Array.map()` to iterate over elements and combine into an array containing all combinations.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const powerset = arr =>
|
const powerset = arr =>
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
### Randomize order of array
|
### Randomize order of array
|
||||||
|
|
||||||
Use `sort()` to reorder elements, utilizing `Math.random()` to randomize the sorting.
|
Use `Array.sort()` to reorder elements, utilizing `Math.random()` to randomize the sorting.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const randomizeOrder = arr => arr.sort( (a,b) => Math.random() >= 0.5 ? -1 : 1);
|
const randomizeOrder = arr => arr.sort( (a,b) => Math.random() >= 0.5 ? -1 : 1);
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
### Sort characters in string (alphabetical)
|
### Sort characters in string (alphabetical)
|
||||||
|
|
||||||
Split the string using `split('')`, `sort()` utilizing `localeCompare()`, recombine using `join('')`.
|
Split the string using `split('')`, `Array.sort()` utilizing `localeCompare()`, recombine using `join('')`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const sortCharactersInString = str =>
|
const sortCharactersInString = str =>
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
### Sum of array of numbers
|
### Sum of array of numbers
|
||||||
|
|
||||||
Use `reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const sum = arr => arr.reduce( (acc , val) => acc + val, 0);
|
const sum = arr => arr.reduce( (acc , val) => acc + val, 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user