update snippets 32-47

This commit is contained in:
Stefan Feješ
2017-12-25 14:10:38 +01:00
committed by Agamemnon Zorbas
parent 014a77224c
commit 1c73c70b4a
16 changed files with 71 additions and 27 deletions

View File

@ -20,6 +20,5 @@ const cleanObj = (obj, keysToKeep = [], childIndicator) => {
```js
const testObj = {a: 1, b: 2, children: {a: 1, b: 2}}
cleanObj(testObj, ["a"],"children")
console.log(testObj) // { a: 1, children : { a: 1}}
cleanObj(testObj, ["a"],"children") // { a: 1, children : { a: 1}}
```

View File

@ -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.
```js
const digitize = n => [...'' + n].map(i => parseInt(i));
// digitize(2334) -> [2, 3, 3, 4]
const digitize = n => [...''+n].map(i => parseInt(i));
```
```js
differenceWith([1, 1.2, 1.5, 3], [1.9, 3], (a,b) => Math.round(a) == Math.round(b)) // [1, 1.2]
```

View File

@ -6,5 +6,8 @@ Use `Math.hypot()` to calculate the Euclidean distance between two points.
```js
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
```

View File

@ -6,5 +6,8 @@ Use ES6 `Set` and the `...rest` operator to discard all duplicated values.
```js
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]
```

View File

@ -10,5 +10,8 @@ const dropElements = (arr, func) => {
while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1);
return arr;
};
// dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4]
```
```js
dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4]
```

View File

@ -6,7 +6,10 @@ Use `Array.slice()` to slice the remove the specified number of elements from th
```js
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) // []
```

View File

@ -16,7 +16,10 @@ const elementIsVisibleInViewport = (el, partiallyVisible = false) => {
((left > 0 && left < innerWidth) || (right > 0 && 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)
```

View File

@ -6,5 +6,8 @@ Use `replace()` to escape special characters.
```js
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// escapeRegExp('(test)') -> \\(test\\)
```
```js
escapeRegExp('(test)') -> \\(test\\)
```

View File

@ -6,5 +6,8 @@ Use `Array.filter()` to create a new array that contains every nth element of a
```js
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 ]
```

View File

@ -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.
```js
const extendHex = shortHex =>
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x + x).join('');
// extendHex('#03f') -> '#0033ff'
// extendHex('05a') -> '#0055aa'
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('')
```
```js
extendHex('#03f') // '#0033ff'
extendHex('05a') // '#0055aa'
```

View File

@ -11,5 +11,8 @@ Throws an exception if `n` is a negative number.
const factorial = n =>
n < 0 ? (() => { throw new TypeError('Negative numbers are not allowed!'); })()
: n <= 1 ? 1 : n * factorial(n - 1);
// factorial(6) -> 720
```
```js
factorial(6) -> 720
```

View File

@ -8,5 +8,8 @@ Use `Array.reduce()` to add values into the array, using the sum of the last two
```js
const fibonacci = n =>
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
```

View File

@ -6,6 +6,9 @@ Use a mathematical formula to calculate the number of fibonacci numbers until `n
```js
const fibonacciCountUntilNum = num =>
Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
// fibonacciCountUntilNum(10) -> 7
Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2));
```
```js
fibonacciCountUntilNum(10) -> 7
```

View File

@ -10,6 +10,9 @@ Uses a mathematical formula to calculate the length of the array required.
const fibonacciUntilNum = num => {
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), []);
};
// fibonacciUntilNum(15) -> [0,1,1,2,3,5,8,13]
}
```
```js
fibonacciCountUntilNum(10) // 7
```

View File

@ -6,5 +6,8 @@ Use `Array.filter()` for an array containing only the unique values.
```js
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]
```

View File

@ -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.
```js
const flatten = arr => [ ].concat(...arr);
// flatten([1,[2],3,4]) -> [1,2,3,4]
const flatten = arr => [ ].concat( ...arr );
```
```js
flatten([1,[2],3,4]) // [1,2,3,4]
```