From ec38b481eb045e79c3c68f3b3ebe8da8f4f3291e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:10:44 +0300 Subject: [PATCH] Remove factors --- snippets_archive/factors.md | 45 ------------------------------------- test/factors.test.js | 17 -------------- 2 files changed, 62 deletions(-) delete mode 100644 snippets_archive/factors.md delete mode 100644 test/factors.test.js diff --git a/snippets_archive/factors.md b/snippets_archive/factors.md deleted file mode 100644 index 0605a6ea1..000000000 --- a/snippets_archive/factors.md +++ /dev/null @@ -1,45 +0,0 @@ ---- -title: factors -tags: math,intermediate ---- - -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.prototype.map()` and `Array.prototype.filter()` to find all the factors of `num`. -If given `num` is negative, use `Array.prototype.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.prototype.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] -``` \ No newline at end of file diff --git a/test/factors.test.js b/test/factors.test.js deleted file mode 100644 index 8fc7f0f5c..000000000 --- a/test/factors.test.js +++ /dev/null @@ -1,17 +0,0 @@ -const {factors} = require('./_30s.js'); - -test('factors is a Function', () => { - expect(factors).toBeInstanceOf(Function); -}); -test('factors returns the correct array', () => { - expect(factors(12)).toEqual([2, 3, 4, 6, 12]); -}); -test('factors returns the correct array of primes', () => { - expect(factors(12, true)).toEqual([2, 3]); -}); -test('factors returns the correct array for negatives', () => { - expect(factors(-12)).toEqual([2, -2, 3, -3, 4, -4, 6, -6, 12, -12]); -}); -test('factors returns the correct array of primes for negatives', () => { - expect(factors(-12, true)).toEqual([2, 3]); -});