diff --git a/README.md b/README.md index 0f0605171..05c69c1c9 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,7 @@ * [`distance`](#distance) * [`elo`](#elo) * [`factorial`](#factorial) +* [`factors`](#factors) * [`fibonacci`](#fibonacci) * [`fibonacciCountUntilNum`](#fibonaccicountuntilnum) * [`fibonacciUntilNum`](#fibonacciuntilnum) @@ -287,15 +288,6 @@ -### _Uncategorized_ - -
-View contents - -* [`factors`](#factors) - -
- --- ## 🔌 Adapter @@ -2816,6 +2808,57 @@ factorial(6); // 720
[⬆ Back to top](#table-of-contents) +### 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; +}; +``` + +
+Examples + +```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] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### fibonacci Generates an array, containing the Fibonacci sequence, up until the nth term. @@ -5212,54 +5255,6 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) ---- - ## _Uncategorized_ - -### 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] -``` - -
[⬆ back to top](#table-of-contents) - ## Collaborators diff --git a/docs/index.html b/docs/index.html index c9fb90a97..199e601e5 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+    }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -611,6 +611,29 @@ elo([1200, 1200], 64); // [1232, 1168]
       })()
     : n <= 1 ? 1 : n * factorial(n - 1);
 
factorial(6); // 720
+

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.

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;
+};
+
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]
 

fibonacci

Generates an array, containing the Fibonacci sequence, up until the nth term.

Create an empty array of the specific length, initializing the first two values (0 and 1). Use Array.reduce() to add values into the array, using the sum of the last two values, except for the first two.

const fibonacci = n =>
   Array.from({ length: n }).reduce(
     (acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
@@ -1179,27 +1202,4 @@ console.log(sdbm('age')); // 808122783
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
-

Uncategorized

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.

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;
-};
-
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]
 

\ No newline at end of file