update snippets 1-15

This commit is contained in:
Stefan Feješ
2017-12-25 13:59:23 +01:00
committed by Agamemnon Zorbas
parent 9b8c9f4bcf
commit b2cffa6858
15 changed files with 72 additions and 31 deletions

View File

@ -13,5 +13,8 @@ const anagrams = str => {
return str.split('').reduce((acc, letter, i) => return str.split('').reduce((acc, letter, i) =>
acc.concat(anagrams(str.slice(0, i) + str.slice(i + 1)).map(val => letter + val)), []); acc.concat(anagrams(str.slice(0, i) + str.slice(i + 1)).map(val => letter + val)), []);
}; };
// anagrams('abc') -> ['abc','acb','bac','bca','cab','cba'] ```
```js
anagrams('abc') -> ['abc','acb','bac','bca','cab','cba']
``` ```

View File

@ -6,5 +6,8 @@ Use `Array.reduce()` to add each value to an accumulator, initialized with a val
```js ```js
const arrayAverage = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length; const arrayAverage = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length;
// arrayAverage([1,2,3]) -> 2 ```
```js
arrayAverage([1,2,3]) -> 2
``` ```

View File

@ -7,8 +7,11 @@ Use `Array.reduce()` and the `gcd` formula (uses recursion) to calculate the gre
```js ```js
const arrayGcd = arr => { const arrayGcd = arr => {
const gcd = (x, y) => !y ? x : gcd(y, x % y); const gcd = (x, y) => !y ? x : gcd(y, x % y);
return arr.reduce((a, b) => gcd(a, b)); return arr.reduce((a,b) => gcd(a,b));
}; }
// arrayGcd([1,2,3,4,5]) -> 1 ```
// arrayGcd([4,8,12]) -> 4
```js
arrayGcd([1,2,3,4,5]) -> 1
arrayGcd([4,8,12]) -> 4
``` ```

View File

@ -7,9 +7,12 @@ Use `Array.reduce()` and the `lcm` formula (uses recursion) to calculate the low
```js ```js
const arrayLcm = arr => { const arrayLcm = arr => {
const gcd = (x, y) => !y ? x : gcd(y, x % y); const gcd = (x, y) => !y ? x : gcd(y, x % y);
const lcm = (x, y) => (x * y) / gcd(x, y); const lcm = (x, y) => (x*y)/gcd(x, y);
return arr.reduce((a, b) => lcm(a, b)); return arr.reduce((a,b) => lcm(a,b));
}; }
// arrayLcm([1,2,3,4,5]) -> 60 ```
// arrayLcm([4,8,12]) -> 24
```js
arrayLcm([1,2,3,4,5]) -> 60
arrayLcm([4,8,12]) -> 24
``` ```

View File

@ -6,5 +6,8 @@ Use `Math.max()` combined with the spread operator (`...`) to get the maximum va
```js ```js
const arrayMax = arr => Math.max(...arr); const arrayMax = arr => Math.max(...arr);
// arrayMax([10, 1, 5]) -> 10 ```
```js
arrayMax([10, 1, 5]) -> 10
``` ```

View File

@ -6,5 +6,8 @@ Use `Math.min()` combined with the spread operator (`...`) to get the minimum va
```js ```js
const arrayMin = arr => Math.min(...arr); const arrayMin = arr => Math.min(...arr);
// arrayMin([10, 1, 5]) -> 1 ```
```js
arrayMin([10, 1, 5]) -> 1
``` ```

View File

@ -6,5 +6,8 @@ Use `Array.reduce()` to add each value to an accumulator, initialized with a val
```js ```js
const arraySum = arr => arr.reduce((acc, val) => acc + val, 0); const arraySum = arr => arr.reduce((acc, val) => acc + val, 0);
// arraySum([1,2,3,4]) -> 10 ```
```js
arraySum([1,2,3,4]) -> 10
``` ```

View File

@ -5,6 +5,9 @@ Converts the given array elements into `<li>` tags and appends them to the list
Use `Array.map()` and `document.querySelector()` to create a list of html tags. Use `Array.map()` and `document.querySelector()` to create a list of html tags.
```js ```js
const arrayToHtmlList = (arr, listID) => arr.map(item => document.querySelector('#' + listID).innerHTML += `<li>${item}</li>`); const arrayToHtmlList = (arr, listID) => arr.map(item => document.querySelector("#"+listID).innerHTML+=`<li>${item}</li>`);
// arrayToHtmlList(['item 1', 'item 2'],'myListID') ```
```js
arrayToHtmlList(['item 1', 'item 2'],'myListID')
``` ```

View File

@ -7,5 +7,8 @@ Use `scrollY`, `scrollHeight` and `clientHeight` to determine if the bottom of t
```js ```js
const bottomVisible = () => const bottomVisible = () =>
document.documentElement.clientHeight + window.scrollY >= (document.documentElement.scrollHeight || document.documentElement.clientHeight); document.documentElement.clientHeight + window.scrollY >= (document.documentElement.scrollHeight || document.documentElement.clientHeight);
```
```js
// bottomVisible() -> true // bottomVisible() -> true
``` ```

View File

@ -5,10 +5,11 @@ Given a key and a set of arguments, call them when given a context. Primarily us
Use a closure to call a stored key with stored arguments. Use a closure to call a stored key with stored arguments.
```js ```js
const call = (key, ...args) => context => context[ key ](...args); const call = ( key, ...args ) => context => context[ key ]( ...args );
/* ```
```js
Promise.resolve( [ 1, 2, 3 ] ).then( call('map', x => 2 * x ) ).then( console.log ) //[ 2, 4, 6 ] Promise.resolve( [ 1, 2, 3 ] ).then( call('map', x => 2 * x ) ).then( console.log ) //[ 2, 4, 6 ]
const map = call.bind(null, 'map') const map = call.bind(null, 'map')
Promise.resolve( [ 1, 2, 3 ] ).then( map( x => 2 * x ) ).then( console.log ) //[ 2, 4, 6 ] Promise.resolve( [ 1, 2, 3 ] ).then( map( x => 2 * x ) ).then( console.log ) //[ 2, 4, 6 ]
*/ ```
```

View File

@ -8,6 +8,9 @@ Omit the `lowerRest` parameter to keep the rest of the string intact, or set it
```js ```js
const capitalize = ([first, ...rest], lowerRest = false) => const capitalize = ([first, ...rest], lowerRest = false) =>
first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join('')); first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join(''));
// capitalize('myName') -> 'MyName' ```
// capitalize('myName', true) -> 'Myname'
```js
capitalize('fooBar') -> 'FooBar'
capitalize('fooBar', true) -> 'Foobar'
``` ```

View File

@ -6,5 +6,8 @@ Use `replace()` to match the first character of each word and `toUpperCase()` to
```js ```js
const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase()); const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase());
// capitalizeEveryWord('hello world!') -> 'Hello World!' ```
```js
capitalizeEveryWord('hello world!') -> 'Hello World!'
``` ```

View File

@ -6,11 +6,12 @@ Loop through an array of functions containing asynchronous events, calling `next
```js ```js
const chainAsync = fns => { let curr = 0; const next = () => fns[curr++](next); next(); }; const chainAsync = fns => { let curr = 0; const next = () => fns[curr++](next); next(); };
/* ```
```js
chainAsync([ chainAsync([
next => { console.log('0 seconds'); setTimeout(next, 1000); }, next => { console.log('0 seconds'); setTimeout(next, 1000); },
next => { console.log('1 second'); setTimeout(next, 1000); }, next => { console.log('1 second'); setTimeout(next, 1000); },
next => { console.log('2 seconds'); } next => { console.log('2 seconds'); }
]) ])
*/
``` ```

View File

@ -9,5 +9,8 @@ If the original array can't be split evenly, the final chunk will contain the re
```js ```js
const chunk = (arr, size) => const chunk = (arr, size) =>
Array.from({length: Math.ceil(arr.length / size)}, (v, i) => arr.slice(i * size, i * size + size)); Array.from({length: Math.ceil(arr.length / size)}, (v, i) => arr.slice(i * size, i * size + size));
// chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],[5]] ```
```js
chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],[5]]
``` ```

View File

@ -1,13 +1,16 @@
### clampNumber ### clampNumber
Clamps `num` within the inclusive range specified by the boundary values `a` and `b` Clamps `num` within the inclusive range specified by the boundary values `a` and `b`.
If `num` falls within the range, return `num`. If `num` falls within the range, return `num`.
Otherwise, return the nearest number in the range. Otherwise, return the nearest number in the range.
```js ```js
const clampNumber = (num, a, b) => Math.max(Math.min(num, Math.max(a,b)),Math.min(a,b)); const clampNumber = (num, a, b) => Math.max(Math.min(num, Math.max(a,b)),Math.min(a,b));
// clampNumber(2, 3, 5) -> 3 ```
// clampNumber(1, -1, -5) -> -1
// clampNumber(3, 2, 4) -> 3 ```js
clampNumber(2, 3, 5) // 3
clampNumber(1, -1, -5) // -1
clampNumber(3, 2, 4) // 3
``` ```