Merge branch 'master' into update-memoize
This commit is contained in:
42
snippets/factors.md
Normal file
42
snippets/factors.md
Normal file
@ -0,0 +1,42 @@
|
||||
### factors
|
||||
|
||||
Returns the array of factors of the given `num`.
|
||||
If the second argument is set to `true` returns only the prime factors of `num`.
|
||||
If `num` is `1` or `0` returns an empty array.
|
||||
If `num` is less than `0` returns all the factors of `-int` together with their additive inverses.
|
||||
|
||||
Use `Array.from()`, `Array.map()` and `Array.filter()` to find all the factors of `num`.
|
||||
If given `num` is negative, use `Array.reduce()` to add the additive inverses to the array.
|
||||
Return all results if `primes` is `false`, else determine and return only the prime factors using `isPrime` and `Array.filter()`.
|
||||
Omit the second argument, `primes`, to return prime and non-prime factors by default.
|
||||
|
||||
**Note**:- _Negative numbers are not considered prime._
|
||||
|
||||
```js
|
||||
const factors = (num, primes = false) => {
|
||||
const isPrime = num => {
|
||||
const boundary = Math.floor(Math.sqrt(num));
|
||||
for (var i = 2; i <= boundary; i++) if (num % i === 0) return false;
|
||||
return num >= 2;
|
||||
};
|
||||
const isNeg = num < 0;
|
||||
num = isNeg ? -num : num;
|
||||
let array = Array.from({ length: num - 1 })
|
||||
.map((val, i) => (num % (i + 2) === 0 ? i + 2 : false))
|
||||
.filter(val => val);
|
||||
if (isNeg)
|
||||
array = array.reduce((acc, val) => {
|
||||
acc.push(val);
|
||||
acc.push(-val);
|
||||
return acc;
|
||||
}, []);
|
||||
return primes ? array.filter(isPrime) : array;
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
factors(12); // [2,3,4,6,12]
|
||||
factors(12, true); // [2,3]
|
||||
factors(-12); // [2, -2, 3, -3, 4, -4, 6, -6, 12, -12]
|
||||
factors(-12, true); // [2,3]
|
||||
```
|
||||
32
snippets/howManyTimes.md
Normal file
32
snippets/howManyTimes.md
Normal file
@ -0,0 +1,32 @@
|
||||
### howManyTimes
|
||||
|
||||
Returns the number of times `num` can be divided by `divisor` (integer or fractional) without getting a fractional answer.
|
||||
Works for both negative and positive integers.
|
||||
|
||||
If `divisor` is `-1` or `1` return `Infinity`.
|
||||
If `divisor` is `-0` or `0` return `0`.
|
||||
Otherwise, keep dividing `num` with `divisor` and incrementing `i`, while the result is an integer.
|
||||
Return the number of times the loop was executed, `i`.
|
||||
|
||||
```js
|
||||
const howManyTimes = (num, divisor) => {
|
||||
if (divisor === 1 || divisor === -1) return Infinity;
|
||||
if (divisor === 0) return 0;
|
||||
let i = 0;
|
||||
while (Number.isInteger(num / divisor)) {
|
||||
i++;
|
||||
num = num / divisor;
|
||||
}
|
||||
return i;
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
howManyTimes(100, 2); //2
|
||||
howManyTimes(100, -2); //2
|
||||
howManyTimes(100, 2.5); //2
|
||||
howManyTimes(100, 3); //0
|
||||
howManyTimes(100, 0); //0
|
||||
howManyTimes(100, 1); //Infinity
|
||||
howManyTimes(100, -1); //Infinity
|
||||
```
|
||||
@ -1,10 +1,11 @@
|
||||
### maxN
|
||||
|
||||
Returns the `n` maximum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in descending order).
|
||||
Returns the `n` maximum elements from the provided array. If `n` is greater than or equal to the provided array's length, then return the original array(sorted in descending order).
|
||||
|
||||
Sort's the array's shallow copy in descending order and returns the first n elements
|
||||
Use `Array.sort()` combined with the spread operator (`...`) to create a shallow clone of the array and sort it in descending order.
|
||||
Use `Array.slice()` to get the specified number of elements.
|
||||
Omit the second argument, `n`, to get a one-element array.
|
||||
|
||||
Skip the second argument to get a single element(in the form of a array)
|
||||
```js
|
||||
const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);
|
||||
```
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
### minN
|
||||
|
||||
Returns the `n` minimum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in ascending order).
|
||||
Returns the `n` minimum elements from the provided array. If `n` is greater than or equal to the provided array's length, then return the original array(sorted in ascending order).
|
||||
|
||||
Sort's the array's shallow copy in ascending order and returns the first n elements
|
||||
Use `Array.sort()` combined with the spread operator (`...`) to create a shallow clone of the array and sort it in ascending order.
|
||||
Use `Array.slice()` to get the specified number of elements.
|
||||
Omit the second argument, `n`, to get a one-element array.
|
||||
|
||||
Skip the second argument to get a single element(in the form of a array)
|
||||
```js
|
||||
const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
|
||||
```
|
||||
|
||||
23
snippets/pluralize.md
Normal file
23
snippets/pluralize.md
Normal file
@ -0,0 +1,23 @@
|
||||
# pluralize
|
||||
|
||||
If `num` is greater than `1` returns the plural form of the given string, else return the singular form.
|
||||
|
||||
Check if `num` is greater than `0`. Throw an appropriate `Error` if not, return the appropriate string otherwise.
|
||||
Omit the third argument, `items`, to use a default plural form same as `item` suffixed with a single `'s'`.
|
||||
|
||||
```js
|
||||
const pluralize = (num, item, items = item + 's') =>
|
||||
num <= 0
|
||||
? (() => {
|
||||
throw new Error(`'num' should be >= 1. Value povided was ${num}.`);
|
||||
})()
|
||||
: num === 1 ? item : items;
|
||||
```
|
||||
|
||||
```js
|
||||
pluralize(1, 'apple', 'apples'); // 'apple'
|
||||
pluralize(3, 'apple', 'apples'); // 'apples'
|
||||
pluralize(2, 'apple'); // 'apples'
|
||||
pluralize(0, 'apple', 'apples'); // Gives error
|
||||
pluralize(-3, 'apple', 'apples'); // Gives error
|
||||
```
|
||||
Reference in New Issue
Block a user