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.
|
||||
|
||||
```js
|
||||
const uuid = () =>
|
||||
const uuidGenerator = () =>
|
||||
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
|
||||
(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.
|
||||
|
||||
```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 ]
|
||||
```
|
||||
@ -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.
|
||||
`Array.slice()` is used to remove `#` from string start since it's added once.
|
||||
```js
|
||||
const convertHex = shortHex =>
|
||||
const extendHex = shortHex =>
|
||||
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split().map(x => x+x).join()
|
||||
// convertHex('#03f') -> '#0033ff'
|
||||
// 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`.
|
||||
|
||||
```js
|
||||
const getScrollPos = (el = window) =>
|
||||
const getScrollPosition = (el = window) =>
|
||||
({x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft,
|
||||
y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollTop});
|
||||
// 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`.
|
||||
|
||||
```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
|
||||
```
|
||||
@ -3,6 +3,6 @@
|
||||
Use `arr.length - 1` to compute index of the last element of the given array and returning it.
|
||||
|
||||
```js
|
||||
const last = arr => arr[arr.length - 1];
|
||||
const lastOfArray = arr => arr[arr.length - 1];
|
||||
// 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.
|
||||
|
||||
```js
|
||||
const lcm = (x,y) => {
|
||||
const leastCommonMultiple = (x,y) => {
|
||||
const gcd = (x, y) => !y ? x : gcd(y, x % y);
|
||||
return Math.abs(x*y)/(gcd(x,y));
|
||||
};
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
### Nth element of array
|
||||
|
||||
Use `Array.slice()` to get an array containing the nth element at the first place.
|
||||
Use `Array.slice()` to get an array containing the nth element at the first place.
|
||||
If the index is out of bounds, return `[]`.
|
||||
Omit the second argument, `n`, to get the first element of the array.
|
||||
|
||||
```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','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.
|
||||
|
||||
```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 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.
|
||||
|
||||
```js
|
||||
const randomInRange = (min, max) => Math.random() * (max - min) + min;
|
||||
const randomNumberInRange = (min, max) => Math.random() * (max - min) + min;
|
||||
// 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.
|
||||
|
||||
```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))
|
||||
// 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.
|
||||
|
||||
```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]
|
||||
```
|
||||
|
||||
@ -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).
|
||||
|
||||
```js
|
||||
const speak = message => {
|
||||
const speechSynthesis = message => {
|
||||
const msg = new SpeechSynthesisUtterance(message);
|
||||
msg.voice = window.speechSynthesis.getVoices()[0];
|
||||
window.speechSynthesis.speak(msg);
|
||||
|
||||
@ -3,6 +3,6 @@
|
||||
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
||||
|
||||
```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
|
||||
```
|
||||
@ -3,7 +3,7 @@
|
||||
Return `arr.slice(1)` if the array's `length` is more than `1`, otherwise return the whole array.
|
||||
|
||||
```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]) -> [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.
|
||||
|
||||
```js
|
||||
const truncate = (str, num) =>
|
||||
const truncateString = (str, num) =>
|
||||
str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str;
|
||||
// truncate('boomerang', 7) -> 'boom...'
|
||||
```
|
||||
@ -3,6 +3,6 @@
|
||||
Use ES6 `Set` and the `...rest` operator to discard all duplicated values.
|
||||
|
||||
```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]
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user