fix naming
This commit is contained in:
@ -3,7 +3,7 @@
|
|||||||
Use `crypto` API to generate a UUID, compliant with [RFC4122](https://www.ietf.org/rfc/rfc4122.txt) version 4.
|
Use `crypto` API to generate a UUID, compliant with [RFC4122](https://www.ietf.org/rfc/rfc4122.txt) version 4.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const uuid = () =>
|
const uuidGenerator = () =>
|
||||||
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
|
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
|
||||||
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
|
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
|
||||||
);
|
);
|
||||||
|
|||||||
@ -3,6 +3,6 @@
|
|||||||
Use `Array.filter()` to create a new array that contains every nth element of a given array.
|
Use `Array.filter()` to create a new array that contains every nth element of a given array.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const everynth = (arr, nth) => arr.filter((e, i) => i % nth === 0);
|
const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === 0);
|
||||||
// everynth([1,2,3,4,5,6], 2) -> [ 1, 3, 5 ]
|
// everynth([1,2,3,4,5,6], 2) -> [ 1, 3, 5 ]
|
||||||
```
|
```
|
||||||
@ -3,7 +3,7 @@
|
|||||||
Use `Array.map()`, `split()` and `Array.join()` to join the mapped array for converting a three-digit RGB notated hexadecimal color-code to the six-digit form.
|
Use `Array.map()`, `split()` and `Array.join()` to join the mapped array for converting a three-digit RGB notated hexadecimal color-code to the six-digit form.
|
||||||
`Array.slice()` is used to remove `#` from string start since it's added once.
|
`Array.slice()` is used to remove `#` from string start since it's added once.
|
||||||
```js
|
```js
|
||||||
const convertHex = 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()
|
||||||
// convertHex('#03f') -> '#0033ff'
|
// convertHex('#03f') -> '#0033ff'
|
||||||
// convertHex('05a') -> '#0055aa'
|
// convertHex('05a') -> '#0055aa'
|
||||||
@ -4,7 +4,7 @@ Use `pageXOffset` and `pageYOffset` if they are defined, otherwise `scrollLeft`
|
|||||||
You can omit `el` to use a default value of `window`.
|
You can omit `el` to use a default value of `window`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const getScrollPos = (el = window) =>
|
const getScrollPosition = (el = window) =>
|
||||||
({x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft,
|
({x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft,
|
||||||
y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollTop});
|
y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollTop});
|
||||||
// getScrollPos() -> {x: 0, y: 200}
|
// getScrollPos() -> {x: 0, y: 200}
|
||||||
|
|||||||
@ -5,6 +5,6 @@ Base case is when `y` equals `0`. In this case, return `x`.
|
|||||||
Otherwise, return the GCD of `y` and the remainder of the division `x/y`.
|
Otherwise, return the GCD of `y` and the remainder of the division `x/y`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const gcd = (x, y) => !y ? x : gcd(y, x % y);
|
const greatestCommonDivisor = (x, y) => !y ? x : gcd(y, x % y);
|
||||||
// gcd (8, 36) -> 4
|
// gcd (8, 36) -> 4
|
||||||
```
|
```
|
||||||
@ -3,6 +3,6 @@
|
|||||||
Use `arr.length - 1` to compute index of the last element of the given array and returning it.
|
Use `arr.length - 1` to compute index of the last element of the given array and returning it.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const last = arr => arr[arr.length - 1];
|
const lastOfArray = arr => arr[arr.length - 1];
|
||||||
// last([1,2,3]) -> 3
|
// last([1,2,3]) -> 3
|
||||||
```
|
```
|
||||||
|
|||||||
@ -4,7 +4,7 @@ Use the greatest common divisor (GCD) formula and `Math.abs()` to determine the
|
|||||||
The GCD formula uses recursion.
|
The GCD formula uses recursion.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const lcm = (x,y) => {
|
const leastCommonMultiple = (x,y) => {
|
||||||
const gcd = (x, y) => !y ? x : gcd(y, x % y);
|
const gcd = (x, y) => !y ? x : gcd(y, x % y);
|
||||||
return Math.abs(x*y)/(gcd(x,y));
|
return Math.abs(x*y)/(gcd(x,y));
|
||||||
};
|
};
|
||||||
|
|||||||
@ -5,7 +5,7 @@ If the index is out of bounds, return `[]`.
|
|||||||
Omit the second argument, `n`, to get the first element of the array.
|
Omit the second argument, `n`, to get the first element of the array.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const nth = (arr, n=0) => (n>0? arr.slice(n,n+1) : arr.slice(n))[0];
|
const nthElement = (arr, n=0) => (n>0? arr.slice(n,n+1) : arr.slice(n))[0];
|
||||||
// nth(['a','b','c'],1) -> 'b'
|
// nth(['a','b','c'],1) -> 'b'
|
||||||
// nth(['a','b','b']-2) -> 'a'
|
// nth(['a','b','b']-2) -> 'a'
|
||||||
```
|
```
|
||||||
@ -4,7 +4,7 @@ Use `Array.reduce()` with the spread operator (`...`) to perform left-to-right f
|
|||||||
The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.
|
The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const pipe = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)));
|
const pipeFunctions = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)));
|
||||||
/*
|
/*
|
||||||
const add5 = x => x + 5
|
const add5 = x => x + 5
|
||||||
const multiply = (x, y) => x * y
|
const multiply = (x, y) => x * y
|
||||||
|
|||||||
@ -3,6 +3,6 @@
|
|||||||
Use `Math.random()` to generate a random value, map it to the desired range using multiplication.
|
Use `Math.random()` to generate a random value, map it to the desired range using multiplication.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const randomInRange = (min, max) => Math.random() * (max - min) + min;
|
const randomNumberInRange = (min, max) => Math.random() * (max - min) + min;
|
||||||
// randomInRange(2,10) -> 6.0211363285087005
|
// randomInRange(2,10) -> 6.0211363285087005
|
||||||
```
|
```
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
Run an array of promises in series using `Array.reduce()` by creating a promise chain, where each promise returns the next promise when resolved.
|
Run an array of promises in series using `Array.reduce()` by creating a promise chain, where each promise returns the next promise when resolved.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const series = ps => ps.reduce((p, next) => p.then(next), Promise.resolve());
|
const runPromisesInSeries = ps => ps.reduce((p, next) => p.then(next), Promise.resolve());
|
||||||
// const delay = (d) => new Promise(r => setTimeout(r, d))
|
// const delay = (d) => new Promise(r => setTimeout(r, d))
|
||||||
// series([() => delay(1000), () => delay(2000)]) -> executes each promise sequentially, taking a total of 3 seconds to complete
|
// series([() => delay(1000), () => delay(2000)]) -> executes each promise sequentially, taking a total of 3 seconds to complete
|
||||||
```
|
```
|
||||||
|
|||||||
@ -3,6 +3,6 @@
|
|||||||
Use `Array.sort()` to reorder elements, using `Math.random()` in the comparator.
|
Use `Array.sort()` to reorder elements, using `Math.random()` in the comparator.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const shuffle = arr => arr.sort(() => Math.random() - 0.5);
|
const shuffleArray = arr => arr.sort(() => Math.random() - 0.5);
|
||||||
// shuffle([1,2,3]) -> [2,3,1]
|
// shuffle([1,2,3]) -> [2,3,1]
|
||||||
```
|
```
|
||||||
|
|||||||
@ -6,7 +6,7 @@ Use `window.speechSynthesis.speak()` to play the message.
|
|||||||
Learn more about the [SpeechSynthesisUtterance interface of the Web Speech API](https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance).
|
Learn more about the [SpeechSynthesisUtterance interface of the Web Speech API](https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance).
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const speak = message => {
|
const speechSynthesis = message => {
|
||||||
const msg = new SpeechSynthesisUtterance(message);
|
const msg = new SpeechSynthesisUtterance(message);
|
||||||
msg.voice = window.speechSynthesis.getVoices()[0];
|
msg.voice = window.speechSynthesis.getVoices()[0];
|
||||||
window.speechSynthesis.speak(msg);
|
window.speechSynthesis.speak(msg);
|
||||||
|
|||||||
@ -3,6 +3,6 @@
|
|||||||
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const sum = arr => arr.reduce((acc, val) => acc + val, 0);
|
const sumArrayNumbers = arr => arr.reduce((acc, val) => acc + val, 0);
|
||||||
// sum([1,2,3,4]) -> 10
|
// sum([1,2,3,4]) -> 10
|
||||||
```
|
```
|
||||||
@ -3,7 +3,7 @@
|
|||||||
Return `arr.slice(1)` if the array's `length` is more than `1`, otherwise return the whole array.
|
Return `arr.slice(1)` if the array's `length` is more than `1`, otherwise return the whole array.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const tail = arr => arr.length > 1 ? arr.slice(1) : arr;
|
const tailOfList = arr => arr.length > 1 ? arr.slice(1) : arr;
|
||||||
// tail([1,2,3]) -> [2,3]
|
// tail([1,2,3]) -> [2,3]
|
||||||
// tail([1]) -> [1]
|
// tail([1]) -> [1]
|
||||||
```
|
```
|
||||||
|
|||||||
@ -4,7 +4,7 @@ Determine if the string's `length` is greater than `num`.
|
|||||||
Return the string truncated to the desired length, with `...` appended to the end or the original string.
|
Return the string truncated to the desired length, with `...` appended to the end or the original string.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const truncate = (str, num) =>
|
const truncateString = (str, num) =>
|
||||||
str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str;
|
str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str;
|
||||||
// truncate('boomerang', 7) -> 'boom...'
|
// truncate('boomerang', 7) -> 'boom...'
|
||||||
```
|
```
|
||||||
@ -3,6 +3,6 @@
|
|||||||
Use ES6 `Set` and the `...rest` operator to discard all duplicated values.
|
Use ES6 `Set` and the `...rest` operator to discard all duplicated values.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const unique = arr => [...new Set(arr)];
|
const uniqueValuesOfArray = arr => [...new Set(arr)];
|
||||||
// unique([1,2,2,3,4,4,5]) -> [1,2,3,4,5]
|
// unique([1,2,2,3,4,4,5]) -> [1,2,3,4,5]
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user