diff --git a/README.md b/README.md index 147797844..e0bfd8aa8 100644 --- a/README.md +++ b/README.md @@ -14,12 +14,14 @@ * [Count occurences of a value in array](#count-occurences-of-a-value-in-array) * [Current URL](#current-url) * [Even or odd number](#even-or-odd-number) +* [Factorial](#factorial) * [Fibonacci array generator](#fibonacci-array-generator) * [Flatten array](#flatten-array) * [Greatest common divisor (GCD)](#greatest-common-divisor-gcd) * [Initialize array with range](#initialize-array-with-range) * [Initialize array with values](#initialize-array-with-values) * [Measure time taken by function](#measure-time-taken-by-function) +* [Powerset](#powerset) * [Random number in range](#random-number-in-range) * [Randomize order of array](#randomize-order-of-array) * [Redirect to url](#redirect-to-url) @@ -92,6 +94,15 @@ Return `true` if the number is even, `false` if the number is odd. var isEven = num => Math.abs(num) % 2 === 0; ``` +### Factorial + +Create an array of length `n+1`, use `reduce()` to get the product of every value in the given range, utilizing the index of each element. + +```js +var factorial = n => + Array.apply(null, [1].concat(Array(n))).reduce( (a, _, i) => a * i || 1 , 1); +``` + ### Fibonacci array generator Create an empty array of the specific length, initializing the first two values (`0` and `1`). @@ -159,6 +170,15 @@ var timeTaken = (f,...args) => { } ``` +### Powerset + +Use `reduce()` combined with `map()` to iterate over elements and combine into an array containing all combinations. + +```js +var powerset = arr => + arr.reduce( (a,v) => a.concat(a.map( r => [v].concat(r) )), [[]]); +``` + ### Random number in range Use `Math.random()` to generate a random value, map it to the desired range using multiplication. diff --git a/snippets/factorial.md b/snippets/factorial.md new file mode 100644 index 000000000..fb60c9204 --- /dev/null +++ b/snippets/factorial.md @@ -0,0 +1,8 @@ +### Factorial + +Create an array of length `n+1`, use `reduce()` to get the product of every value in the given range, utilizing the index of each element. + +```js +var factorial = n => + Array.apply(null, [1].concat(Array(n))).reduce( (a, _, i) => a * i || 1 , 1); +``` diff --git a/snippets/powerset.md b/snippets/powerset.md new file mode 100644 index 000000000..53c98a3a1 --- /dev/null +++ b/snippets/powerset.md @@ -0,0 +1,8 @@ +### Powerset + +Use `reduce()` combined with `map()` to iterate over elements and combine into an array containing all combinations. + +```js +var powerset = arr => + arr.reduce( (a,v) => a.concat(a.map( r => [v].concat(r) )), [[]]); +```