Merge branch 'master' of https://github.com/Chalarangelo/30-seconds-of-code into add-chunkArray
This commit is contained in:
50
README.md
50
README.md
@ -16,6 +16,7 @@
|
|||||||
* [Count occurrences of a value in array](#count-occurrences-of-a-value-in-array)
|
* [Count occurrences of a value in array](#count-occurrences-of-a-value-in-array)
|
||||||
* [Current URL](#current-url)
|
* [Current URL](#current-url)
|
||||||
* [Curry](#curry)
|
* [Curry](#curry)
|
||||||
|
* [Deep flatten array](#deep-flatten-array)
|
||||||
* [Difference between arrays](#difference-between-arrays)
|
* [Difference between arrays](#difference-between-arrays)
|
||||||
* [Distance between two points](#distance-between-two-points)
|
* [Distance between two points](#distance-between-two-points)
|
||||||
* [Divisible by number](#divisible-by-number)
|
* [Divisible by number](#divisible-by-number)
|
||||||
@ -34,6 +35,7 @@
|
|||||||
* [Last of list](#last-of-list)
|
* [Last of list](#last-of-list)
|
||||||
* [Measure time taken by function](#measure-time-taken-by-function)
|
* [Measure time taken by function](#measure-time-taken-by-function)
|
||||||
* [Object from key value pairs](#object-from-key-value-pairs)
|
* [Object from key value pairs](#object-from-key-value-pairs)
|
||||||
|
* [Pipe](#pipe)
|
||||||
* [Powerset](#powerset)
|
* [Powerset](#powerset)
|
||||||
* [Random number in range](#random-number-in-range)
|
* [Random number in range](#random-number-in-range)
|
||||||
* [Randomize order of array](#randomize-order-of-array)
|
* [Randomize order of array](#randomize-order-of-array)
|
||||||
@ -115,7 +117,7 @@ const palindrome = str =>
|
|||||||
Use `reduce()` to increment a counter each time you encounter the specific value inside the array.
|
Use `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);
|
||||||
// countOccurrences([1,1,2,1,2,3], 1) -> 3
|
// countOccurrences([1,1,2,1,2,3], 1) -> 3
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -141,6 +143,17 @@ const curry = f =>
|
|||||||
// curry(Math.pow)(2)(10) -> 1024
|
// curry(Math.pow)(2)(10) -> 1024
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Deep flatten array
|
||||||
|
|
||||||
|
Use recursion.
|
||||||
|
Use `reduce()` to get all elements that are not arrays, flatten each element that is an array.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const deepFlatten = arr =>
|
||||||
|
arr.reduce( (a, v) => a.concat( Array.isArray(v) ? flatten(v) : v ), []);
|
||||||
|
// deepFlatten([1,[2],[[3],4],5]) -> [1,2,3,4,5]
|
||||||
|
```
|
||||||
|
|
||||||
### Difference between arrays
|
### Difference between arrays
|
||||||
|
|
||||||
Use `filter()` to remove values that are part of `values`, determined using `includes()`.
|
Use `filter()` to remove values that are part of `values`, determined using `includes()`.
|
||||||
@ -173,8 +186,7 @@ const isDivisible = (dividend, divisor) => dividend % divisor === 0;
|
|||||||
Use `replace()` to escape special characters.
|
Use `replace()` to escape special characters.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const escapeRegExp = s =>
|
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||||
s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
||||||
// escapeRegExp('(test)') -> \\(test\\)
|
// escapeRegExp('(test)') -> \\(test\\)
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -225,16 +237,14 @@ const unique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
|
|||||||
|
|
||||||
### Flatten array
|
### Flatten array
|
||||||
|
|
||||||
Use recursion.
|
Use `reduce()` to get all elements inside the array and `concat()` to flatten them.
|
||||||
Use `reduce()` to get all elements that are not arrays, flatten each element that is an array.
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const flatten = arr =>
|
const flatten = arr => arr.reduce( (a, v) => a.concat(v), []);
|
||||||
arr.reduce( (a, v) => a.concat( Array.isArray(v) ? flatten(v) : v ), []);
|
// flatten([1,[2],3,4) -> [1,2,3,4]
|
||||||
// flatten([1,[2],[[3],4],5]) -> [1,2,3,4,5]
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Get scroll position
|
### Get scroll position
|
||||||
|
|
||||||
Use `pageXOffset` and `pageYOffset` if they are defined, otherwise `scrollLeft` and `scrollTop`.
|
Use `pageXOffset` and `pageYOffset` if they are defined, otherwise `scrollLeft` and `scrollTop`.
|
||||||
You can omit `el` to use a default value of `window`.
|
You can omit `el` to use a default value of `window`.
|
||||||
@ -289,10 +299,10 @@ const initializeArrayRange = (end, start = 0) =>
|
|||||||
### Initialize array with values
|
### Initialize array with values
|
||||||
|
|
||||||
Use `Array(n)` to create an array of the desired length, `fill(v)` to fill it with the desired values.
|
Use `Array(n)` to create an array of the desired length, `fill(v)` to fill it with the desired values.
|
||||||
You can omit `v` to use a default value of `0`.
|
You can omit `value` to use a default value of `0`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const initializeArray = (n, v = 0) => Array(n).fill(v);
|
const initializeArray = (n, value = 0) => Array(n).fill(value);
|
||||||
// initializeArray(5, 2) -> [2,2,2,2,2]
|
// initializeArray(5, 2) -> [2,2,2,2,2]
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -311,8 +321,8 @@ Use `performance.now()` to get start and end time for the function, `console.log
|
|||||||
First argument is the function name, subsequent arguments are passed to the function.
|
First argument is the function name, subsequent arguments are passed to the function.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const timeTaken = (f,...args) => {
|
const timeTaken = (func,...args) => {
|
||||||
var t0 = performance.now(), r = f(...args);
|
var t0 = performance.now(), r = func(...args);
|
||||||
console.log(performance.now() - t0);
|
console.log(performance.now() - t0);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@ -324,10 +334,19 @@ const timeTaken = (f,...args) => {
|
|||||||
Use `Array.reduce()` to create and combine key-value pairs.
|
Use `Array.reduce()` to create and combine key-value pairs.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const objectFromPairs = arr => arr.reduce((a,b) => (a[b[0]] = b[1], a), {});
|
const objectFromPairs = arr => arr.reduce((a,v) => (a[v[0]] = v[1], a), {});
|
||||||
// objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2}
|
// objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Pipe
|
||||||
|
|
||||||
|
Use `Array.reduce()` to pass value through functions.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const pipe = (...funcs) => arg => funcs.reduce((acc, func) => func(acc), arg);
|
||||||
|
// pipe(btoa, x => x.toUpperCase())("Test") -> "VGVZDA=="
|
||||||
|
```
|
||||||
|
|
||||||
### Powerset
|
### Powerset
|
||||||
|
|
||||||
Use `reduce()` combined with `map()` to iterate over elements and combine into an array containing all combinations.
|
Use `reduce()` combined with `map()` to iterate over elements and combine into an array containing all combinations.
|
||||||
@ -428,8 +447,7 @@ const sortCharactersInString = str =>
|
|||||||
Use `reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
Use `reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const sum = arr =>
|
const sum = arr => arr.reduce( (acc , val) => acc + val, 0);
|
||||||
arr.reduce( (acc , val) => acc + val, 0);
|
|
||||||
// sum([1,2,3,4]) -> 10
|
// sum([1,2,3,4]) -> 10
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@ -3,6 +3,6 @@
|
|||||||
Use `reduce()` to increment a counter each time you encounter the specific value inside the array.
|
Use `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);
|
||||||
// countOccurrences([1,1,2,1,2,3], 1) -> 3
|
// countOccurrences([1,1,2,1,2,3], 1) -> 3
|
||||||
```
|
```
|
||||||
|
|||||||
10
snippets/deep-flatten-array.md
Normal file
10
snippets/deep-flatten-array.md
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
### Deep flatten array
|
||||||
|
|
||||||
|
Use recursion.
|
||||||
|
Use `reduce()` to get all elements that are not arrays, flatten each element that is an array.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const deepFlatten = arr =>
|
||||||
|
arr.reduce( (a, v) => a.concat( Array.isArray(v) ? flatten(v) : v ), []);
|
||||||
|
// deepFlatten([1,[2],[[3],4],5]) -> [1,2,3,4,5]
|
||||||
|
```
|
||||||
@ -3,7 +3,6 @@
|
|||||||
Use `replace()` to escape special characters.
|
Use `replace()` to escape special characters.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const escapeRegExp = s =>
|
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||||
s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
||||||
// escapeRegExp('(test)') -> \\(test\\)
|
// escapeRegExp('(test)') -> \\(test\\)
|
||||||
```
|
```
|
||||||
|
|||||||
@ -1,10 +1,8 @@
|
|||||||
### Flatten array
|
### Flatten array
|
||||||
|
|
||||||
Use recursion.
|
Use `reduce()` to get all elements inside the array and `concat()` to flatten them.
|
||||||
Use `reduce()` to get all elements that are not arrays, flatten each element that is an array.
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const flatten = arr =>
|
const flatten = arr => arr.reduce( (a, v) => a.concat(v), []);
|
||||||
arr.reduce( (a, v) => a.concat( Array.isArray(v) ? flatten(v) : v ), []);
|
// flatten([1,[2],3,4) -> [1,2,3,4]
|
||||||
// flatten([1,[2],[[3],4],5]) -> [1,2,3,4,5]
|
|
||||||
```
|
```
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
## Get scroll position
|
### Get scroll position
|
||||||
|
|
||||||
Use `pageXOffset` and `pageYOffset` if they are defined, otherwise `scrollLeft` and `scrollTop`.
|
Use `pageXOffset` and `pageYOffset` if they are defined, otherwise `scrollLeft` and `scrollTop`.
|
||||||
You can omit `el` to use a default value of `window`.
|
You can omit `el` to use a default value of `window`.
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
### Initialize array with values
|
### Initialize array with values
|
||||||
|
|
||||||
Use `Array(n)` to create an array of the desired length, `fill(v)` to fill it with the desired values.
|
Use `Array(n)` to create an array of the desired length, `fill(v)` to fill it with the desired values.
|
||||||
You can omit `v` to use a default value of `0`.
|
You can omit `value` to use a default value of `0`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const initializeArray = (n, v = 0) => Array(n).fill(v);
|
const initializeArray = (n, value = 0) => Array(n).fill(value);
|
||||||
// initializeArray(5, 2) -> [2,2,2,2,2]
|
// initializeArray(5, 2) -> [2,2,2,2,2]
|
||||||
```
|
```
|
||||||
|
|||||||
@ -4,8 +4,8 @@ Use `performance.now()` to get start and end time for the function, `console.log
|
|||||||
First argument is the function name, subsequent arguments are passed to the function.
|
First argument is the function name, subsequent arguments are passed to the function.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const timeTaken = (f,...args) => {
|
const timeTaken = (func,...args) => {
|
||||||
var t0 = performance.now(), r = f(...args);
|
var t0 = performance.now(), r = func(...args);
|
||||||
console.log(performance.now() - t0);
|
console.log(performance.now() - t0);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,6 @@
|
|||||||
Use `Array.reduce()` to create and combine key-value pairs.
|
Use `Array.reduce()` to create and combine key-value pairs.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const objectFromPairs = arr => arr.reduce((a,b) => (a[b[0]] = b[1], a), {});
|
const objectFromPairs = arr => arr.reduce((a,v) => (a[v[0]] = v[1], a), {});
|
||||||
// objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2}
|
// objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2}
|
||||||
```
|
```
|
||||||
|
|||||||
8
snippets/pipe.md
Normal file
8
snippets/pipe.md
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
### Pipe
|
||||||
|
|
||||||
|
Use `Array.reduce()` to pass value through functions.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const pipe = (...funcs) => arg => funcs.reduce((acc, func) => func(acc), arg);
|
||||||
|
// pipe(btoa, x => x.toUpperCase())("Test") -> "VGVZDA=="
|
||||||
|
```
|
||||||
@ -3,7 +3,6 @@
|
|||||||
Use `reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
Use `reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const sum = arr =>
|
const sum = arr => arr.reduce( (acc , val) => acc + val, 0);
|
||||||
arr.reduce( (acc , val) => acc + val, 0);
|
|
||||||
// sum([1,2,3,4]) -> 10
|
// sum([1,2,3,4]) -> 10
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user