update snippets 1-15
This commit is contained in:
committed by
Agamemnon Zorbas
parent
9b8c9f4bcf
commit
b2cffa6858
@ -13,5 +13,8 @@ const anagrams = str => {
|
||||
return str.split('').reduce((acc, letter, i) =>
|
||||
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']
|
||||
```
|
||||
|
||||
@ -6,5 +6,8 @@ Use `Array.reduce()` to add each value to an accumulator, initialized with a val
|
||||
|
||||
```js
|
||||
const arrayAverage = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length;
|
||||
// arrayAverage([1,2,3]) -> 2
|
||||
```
|
||||
|
||||
```js
|
||||
arrayAverage([1,2,3]) -> 2
|
||||
```
|
||||
|
||||
@ -7,8 +7,11 @@ Use `Array.reduce()` and the `gcd` formula (uses recursion) to calculate the gre
|
||||
```js
|
||||
const arrayGcd = arr => {
|
||||
const gcd = (x, y) => !y ? x : gcd(y, x % y);
|
||||
return arr.reduce((a, b) => gcd(a, b));
|
||||
};
|
||||
// arrayGcd([1,2,3,4,5]) -> 1
|
||||
// arrayGcd([4,8,12]) -> 4
|
||||
return arr.reduce((a,b) => gcd(a,b));
|
||||
}
|
||||
```
|
||||
|
||||
```js
|
||||
arrayGcd([1,2,3,4,5]) -> 1
|
||||
arrayGcd([4,8,12]) -> 4
|
||||
```
|
||||
|
||||
@ -7,9 +7,12 @@ Use `Array.reduce()` and the `lcm` formula (uses recursion) to calculate the low
|
||||
```js
|
||||
const arrayLcm = arr => {
|
||||
const gcd = (x, y) => !y ? x : gcd(y, x % y);
|
||||
const lcm = (x, y) => (x * y) / gcd(x, y);
|
||||
return arr.reduce((a, b) => lcm(a, b));
|
||||
};
|
||||
// arrayLcm([1,2,3,4,5]) -> 60
|
||||
// arrayLcm([4,8,12]) -> 24
|
||||
const lcm = (x, y) => (x*y)/gcd(x, y);
|
||||
return arr.reduce((a,b) => lcm(a,b));
|
||||
}
|
||||
```
|
||||
|
||||
```js
|
||||
arrayLcm([1,2,3,4,5]) -> 60
|
||||
arrayLcm([4,8,12]) -> 24
|
||||
```
|
||||
|
||||
@ -6,5 +6,8 @@ Use `Math.max()` combined with the spread operator (`...`) to get the maximum va
|
||||
|
||||
```js
|
||||
const arrayMax = arr => Math.max(...arr);
|
||||
// arrayMax([10, 1, 5]) -> 10
|
||||
```
|
||||
|
||||
```js
|
||||
arrayMax([10, 1, 5]) -> 10
|
||||
```
|
||||
|
||||
@ -6,5 +6,8 @@ Use `Math.min()` combined with the spread operator (`...`) to get the minimum va
|
||||
|
||||
```js
|
||||
const arrayMin = arr => Math.min(...arr);
|
||||
// arrayMin([10, 1, 5]) -> 1
|
||||
```
|
||||
|
||||
```js
|
||||
arrayMin([10, 1, 5]) -> 1
|
||||
```
|
||||
|
||||
@ -6,5 +6,8 @@ Use `Array.reduce()` to add each value to an accumulator, initialized with a val
|
||||
|
||||
```js
|
||||
const arraySum = arr => arr.reduce((acc, val) => acc + val, 0);
|
||||
// arraySum([1,2,3,4]) -> 10
|
||||
```
|
||||
|
||||
```js
|
||||
arraySum([1,2,3,4]) -> 10
|
||||
```
|
||||
|
||||
@ -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.
|
||||
|
||||
```js
|
||||
const arrayToHtmlList = (arr, listID) => arr.map(item => document.querySelector('#' + listID).innerHTML += `<li>${item}</li>`);
|
||||
// arrayToHtmlList(['item 1', 'item 2'],'myListID')
|
||||
const arrayToHtmlList = (arr, listID) => arr.map(item => document.querySelector("#"+listID).innerHTML+=`<li>${item}</li>`);
|
||||
```
|
||||
|
||||
```js
|
||||
arrayToHtmlList(['item 1', 'item 2'],'myListID')
|
||||
```
|
||||
|
||||
@ -7,5 +7,8 @@ Use `scrollY`, `scrollHeight` and `clientHeight` to determine if the bottom of t
|
||||
```js
|
||||
const bottomVisible = () =>
|
||||
document.documentElement.clientHeight + window.scrollY >= (document.documentElement.scrollHeight || document.documentElement.clientHeight);
|
||||
```
|
||||
|
||||
```js
|
||||
// bottomVisible() -> true
|
||||
```
|
||||
|
||||
@ -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.
|
||||
|
||||
```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 ]
|
||||
const map = call.bind(null, 'map')
|
||||
Promise.resolve( [ 1, 2, 3 ] ).then( map( x => 2 * x ) ).then( console.log ) //[ 2, 4, 6 ]
|
||||
*/
|
||||
```
|
||||
|
||||
@ -8,6 +8,9 @@ Omit the `lowerRest` parameter to keep the rest of the string intact, or set it
|
||||
```js
|
||||
const capitalize = ([first, ...rest], lowerRest = false) =>
|
||||
first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join(''));
|
||||
// capitalize('myName') -> 'MyName'
|
||||
// capitalize('myName', true) -> 'Myname'
|
||||
```
|
||||
|
||||
```js
|
||||
capitalize('fooBar') -> 'FooBar'
|
||||
capitalize('fooBar', true) -> 'Foobar'
|
||||
```
|
||||
|
||||
@ -6,5 +6,8 @@ Use `replace()` to match the first character of each word and `toUpperCase()` to
|
||||
|
||||
```js
|
||||
const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase());
|
||||
// capitalizeEveryWord('hello world!') -> 'Hello World!'
|
||||
```
|
||||
|
||||
```js
|
||||
capitalizeEveryWord('hello world!') -> 'Hello World!'
|
||||
```
|
||||
|
||||
@ -6,11 +6,12 @@ Loop through an array of functions containing asynchronous events, calling `next
|
||||
|
||||
```js
|
||||
const chainAsync = fns => { let curr = 0; const next = () => fns[curr++](next); next(); };
|
||||
/*
|
||||
```
|
||||
|
||||
```js
|
||||
chainAsync([
|
||||
next => { console.log('0 seconds'); setTimeout(next, 1000); },
|
||||
next => { console.log('1 second'); setTimeout(next, 1000); },
|
||||
next => { console.log('2 seconds'); }
|
||||
])
|
||||
*/
|
||||
```
|
||||
|
||||
@ -9,5 +9,8 @@ If the original array can't be split evenly, the final chunk will contain the re
|
||||
```js
|
||||
const chunk = (arr, 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]]
|
||||
```
|
||||
|
||||
@ -1,13 +1,16 @@
|
||||
### 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`.
|
||||
Otherwise, return the nearest number in the range.
|
||||
|
||||
```js
|
||||
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
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user