update snippets 32-47
This commit is contained in:
committed by
Agamemnon Zorbas
parent
014a77224c
commit
1c73c70b4a
@ -20,6 +20,5 @@ const cleanObj = (obj, keysToKeep = [], childIndicator) => {
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const testObj = {a: 1, b: 2, children: {a: 1, b: 2}}
|
const testObj = {a: 1, b: 2, children: {a: 1, b: 2}}
|
||||||
cleanObj(testObj, ["a"],"children")
|
cleanObj(testObj, ["a"],"children") // { a: 1, children : { a: 1}}
|
||||||
console.log(testObj) // { a: 1, children : { a: 1}}
|
|
||||||
```
|
```
|
||||||
|
|||||||
@ -6,6 +6,9 @@ Convert the number to a string, using spread operators in ES6(`[...string]`) bui
|
|||||||
Use `Array.map()` and `parseInt()` to transform each value to an integer.
|
Use `Array.map()` and `parseInt()` to transform each value to an integer.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const digitize = n => [...'' + n].map(i => parseInt(i));
|
const digitize = n => [...''+n].map(i => parseInt(i));
|
||||||
// digitize(2334) -> [2, 3, 3, 4]
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
differenceWith([1, 1.2, 1.5, 3], [1.9, 3], (a,b) => Math.round(a) == Math.round(b)) // [1, 1.2]
|
||||||
```
|
```
|
||||||
|
|||||||
@ -6,5 +6,8 @@ Use `Math.hypot()` to calculate the Euclidean distance between two points.
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
|
const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
|
||||||
// distance(1,1, 2,3) -> 2.23606797749979
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
distance(1,1, 2,3) -> 2.23606797749979
|
||||||
```
|
```
|
||||||
|
|||||||
@ -6,5 +6,8 @@ Use ES6 `Set` and the `...rest` operator to discard all duplicated values.
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const distinctValuesOfArray = arr => [...new Set(arr)];
|
const distinctValuesOfArray = arr => [...new Set(arr)];
|
||||||
// distinctValuesOfArray([1,2,2,3,4,4,5]) -> [1,2,3,4,5]
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
distinctValuesOfArray([1,2,2,3,4,4,5]) -> [1,2,3,4,5]
|
||||||
```
|
```
|
||||||
|
|||||||
@ -10,5 +10,8 @@ const dropElements = (arr, func) => {
|
|||||||
while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1);
|
while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1);
|
||||||
return arr;
|
return arr;
|
||||||
};
|
};
|
||||||
// dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4]
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4]
|
||||||
```
|
```
|
||||||
|
|||||||
@ -6,7 +6,10 @@ Use `Array.slice()` to slice the remove the specified number of elements from th
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const dropRight = (arr, n = 1) => arr.slice(0, -n);
|
const dropRight = (arr, n = 1) => arr.slice(0, -n);
|
||||||
// dropRight([1,2,3]) -> [1,2]
|
```
|
||||||
// dropRight([1,2,3], 2) -> [1]
|
|
||||||
// dropRight([1,2,3], 42) -> []
|
```js
|
||||||
|
dropRight([1,2,3]) // [1,2]
|
||||||
|
dropRight([1,2,3], 2) // [1]
|
||||||
|
dropRight([1,2,3], 42) // []
|
||||||
```
|
```
|
||||||
|
|||||||
@ -16,7 +16,10 @@ const elementIsVisibleInViewport = (el, partiallyVisible = false) => {
|
|||||||
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
|
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
|
||||||
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
|
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
|
||||||
};
|
};
|
||||||
// e.g. 100x100 viewport and a 10x10px element at position {top: -1, left: 0, bottom: 9, right: 10}
|
```
|
||||||
// elementIsVisibleInViewport(el) -> false (not fully visible)
|
|
||||||
// elementIsVisibleInViewport(el, true) -> true (partially visible)
|
```js
|
||||||
|
// e.g. 100x100 viewport and a 10x10px element at position {top: -1, left: 0, bottom: 9, right: 10}
|
||||||
|
elementIsVisibleInViewport(el) -> false // (not fully visible)
|
||||||
|
elementIsVisibleInViewport(el, true) -> true // (partially visible)
|
||||||
```
|
```
|
||||||
|
|||||||
@ -6,5 +6,8 @@ Use `replace()` to escape special characters.
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||||
// escapeRegExp('(test)') -> \\(test\\)
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
escapeRegExp('(test)') -> \\(test\\)
|
||||||
```
|
```
|
||||||
|
|||||||
@ -6,5 +6,8 @@ Use `Array.filter()` to create a new array that contains every nth element of a
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
|
const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
|
||||||
// everyNth([1,2,3,4,5,6], 2) -> [ 2, 4, 6 ]
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
everyNth([1,2,3,4,5,6], 2) -> [ 2, 4, 6 ]
|
||||||
```
|
```
|
||||||
|
|||||||
@ -6,7 +6,10 @@ Use `Array.map()`, `split()` and `Array.join()` to join the mapped array for con
|
|||||||
`String.slice()` is used to remove `#` from string start since it's added once.
|
`String.slice()` is used to remove `#` from string start since it's added once.
|
||||||
```js
|
```js
|
||||||
const extendHex = shortHex =>
|
const extendHex = shortHex =>
|
||||||
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x + x).join('');
|
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('')
|
||||||
// extendHex('#03f') -> '#0033ff'
|
```
|
||||||
// extendHex('05a') -> '#0055aa'
|
|
||||||
|
```js
|
||||||
|
extendHex('#03f') // '#0033ff'
|
||||||
|
extendHex('05a') // '#0055aa'
|
||||||
```
|
```
|
||||||
|
|||||||
@ -11,5 +11,8 @@ Throws an exception if `n` is a negative number.
|
|||||||
const factorial = n =>
|
const factorial = n =>
|
||||||
n < 0 ? (() => { throw new TypeError('Negative numbers are not allowed!'); })()
|
n < 0 ? (() => { throw new TypeError('Negative numbers are not allowed!'); })()
|
||||||
: n <= 1 ? 1 : n * factorial(n - 1);
|
: n <= 1 ? 1 : n * factorial(n - 1);
|
||||||
// factorial(6) -> 720
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
factorial(6) -> 720
|
||||||
```
|
```
|
||||||
|
|||||||
@ -8,5 +8,8 @@ Use `Array.reduce()` to add values into the array, using the sum of the last two
|
|||||||
```js
|
```js
|
||||||
const fibonacci = n =>
|
const fibonacci = n =>
|
||||||
Array.from({ length: n}).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []);
|
Array.from({ length: n}).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []);
|
||||||
// fibonacci(5) -> [0,1,1,2,3]
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
factorial(6) -> 720
|
||||||
```
|
```
|
||||||
|
|||||||
@ -6,6 +6,9 @@ Use a mathematical formula to calculate the number of fibonacci numbers until `n
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const fibonacciCountUntilNum = num =>
|
const fibonacciCountUntilNum = num =>
|
||||||
Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
|
Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2));
|
||||||
// fibonacciCountUntilNum(10) -> 7
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
fibonacciCountUntilNum(10) -> 7
|
||||||
```
|
```
|
||||||
|
|||||||
@ -10,6 +10,9 @@ Uses a mathematical formula to calculate the length of the array required.
|
|||||||
const fibonacciUntilNum = num => {
|
const fibonacciUntilNum = num => {
|
||||||
let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
|
let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
|
||||||
return Array.from({ length: n}).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []);
|
return Array.from({ length: n}).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []);
|
||||||
};
|
}
|
||||||
// fibonacciUntilNum(15) -> [0,1,1,2,3,5,8,13]
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
fibonacciCountUntilNum(10) // 7
|
||||||
```
|
```
|
||||||
|
|||||||
@ -6,5 +6,8 @@ Use `Array.filter()` for an array containing only the unique values.
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
|
const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
|
||||||
// filterNonUnique([1,2,2,3,4,4,5]) -> [1,3,5]
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
filterNonUnique([1,2,2,3,4,4,5]) -> [1,3,5]
|
||||||
```
|
```
|
||||||
|
|||||||
@ -5,6 +5,9 @@ Flattens an array.
|
|||||||
Use a new array and concatenate it with the spread input array causing a shallow denesting of any contained arrays.
|
Use a new array and concatenate it with the spread input array causing a shallow denesting of any contained arrays.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const flatten = arr => [ ].concat(...arr);
|
const flatten = arr => [ ].concat( ...arr );
|
||||||
// flatten([1,[2],3,4]) -> [1,2,3,4]
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
flatten([1,[2],3,4]) // [1,2,3,4]
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user