Merge pull request #1 from Chalarangelo/master

hhj
This commit is contained in:
Rohit Tanwar
2017-12-29 17:59:56 +05:30
committed by GitHub
7 changed files with 236 additions and 213 deletions

261
README.md
View File

@ -32,10 +32,6 @@
<details>
<summary>View contents</summary>
* [`arrayGcd`](#arraygcd)
* [`arrayLcm`](#arraylcm)
* [`arrayMax`](#arraymax)
* [`arrayMin`](#arraymin)
* [`chunk`](#chunk)
* [`compact`](#compact)
* [`countOccurrences`](#countoccurrences)
@ -144,8 +140,7 @@
<details>
<summary>View contents</summary>
* [`arrayAverage`](#arrayaverage)
* [`arraySum`](#arraysum)
* [`average`](#average)
* [`clampNumber`](#clampnumber)
* [`collatz`](#collatz)
* [`digitize`](#digitize)
@ -162,7 +157,9 @@
* [`isEven`](#iseven)
* [`isPrime`](#isprime)
* [`lcm`](#lcm)
* [`max`](#max)
* [`median`](#median)
* [`min`](#min)
* [`palindrome`](#palindrome)
* [`percentile`](#percentile)
* [`powerset`](#powerset)
@ -171,6 +168,7 @@
* [`randomNumberInRange`](#randomnumberinrange)
* [`round`](#round)
* [`standardDeviation`](#standarddeviation)
* [`sum`](#sum)
</details>
@ -427,107 +425,6 @@ arrayMax([1, 2, 4]); // 4
## Array
### arrayGcd
Calculates the greatest common denominator (gcd) of an array of numbers.
Use `Array.reduce()` and the `gcd` formula (uses recursion) to calculate the greatest common denominator of an array of numbers.
```js
const arrayGcd = arr => {
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
return arr.reduce((a, b) => gcd(a, b));
};
```
<details>
<summary>Examples</summary>
```js
arrayGcd([1, 2, 3, 4, 5]); // 1
arrayGcd([4, 8, 12]); // 4
```
</details>
[⬆ Back to top](#table-of-contents)
### arrayLcm
Calculates the lowest common multiple (lcm) of an array of numbers.
Use `Array.reduce()` and the `lcm` formula (uses recursion) to calculate the lowest common multiple of an array of numbers.
```js
const arrayLcm = arr => {
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
const lcm = (x, y) => x * y / gcd(x, y);
return arr.reduce((a, b) => lcm(a, b));
};
```
<details>
<summary>Examples</summary>
```js
arrayLcm([1, 2, 3, 4, 5]); // 60
arrayLcm([4, 8, 12]); // 24
```
</details>
[⬆ Back to top](#table-of-contents)
### arrayMax
Returns the maximum value in an array.
Use `Math.max()` combined with the spread operator (`...`) to get the maximum value in the array.
```js
const arrayMax = arr => Math.max(...arr);
```
<details>
<summary>Examples</summary>
```js
arrayMax([10, 1, 5]); // 10
```
</details>
[⬆ Back to top](#table-of-contents)
### arrayMin
Returns the minimum value in an array.
Use `Math.min()` combined with the spread operator (`...`) to get the minimum value in the array.
```js
const arrayMin = arr => Math.min(...arr);
```
<details>
<summary>Examples</summary>
```js
arrayMin([10, 1, 5]); // 1
```
</details>
[⬆ Back to top](#table-of-contents)
### chunk
Chunks an array into smaller arrays of a specified size.
@ -2308,44 +2205,21 @@ negate(isOdd)(1); // false
## Math
### arrayAverage
### average
Returns the average of an array of numbers.
Returns the average of an of two or more numbers/arrays.
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.
```js
const arrayAverage = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length;
const average = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0) / arr.length;
```
<details>
<summary>Examples</summary>
```js
arrayAverage([1, 2, 3]); // 2
```
</details>
[⬆ Back to top](#table-of-contents)
### arraySum
Returns the sum of an array of numbers.
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`.
```js
const arraySum = arr => arr.reduce((acc, val) => acc + val, 0);
```
<details>
<summary>Examples</summary>
```js
arraySum([1, 2, 3, 4]); // 10
average([1, 2, 3]); // 2
```
</details>
@ -2567,14 +2441,18 @@ fibonacciCountUntilNum(10); // 7
### gcd
Calculates the greatest common divisor between two numbers.
Calculates the greatest common divisor between two or more numbers/arrays.
Use recursion.
The `helperGcd `function uses recursion.
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 gcd = (...arr) => {
let data = [].concat(...arr);
const helperGcd = (x, y) => (!y ? x : gcd(y, x % y));
return data.reduce((a, b) => helperGcd(a, b));
};
```
<details>
@ -2750,15 +2628,17 @@ isPrime(12); // false
### lcm
Returns the least common multiple of two numbers.
Returns the least common multiple of two or numbers/arrays.
Use the greatest common divisor (GCD) formula and `Math.abs()` to determine the least common multiple.
The GCD formula uses recursion.
```js
const lcm = (x, y) => {
const lcm = (...arr) => {
let data = [].concat(...arr);
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
return Math.abs(x * y) / gcd(x, y);
const helperLcm = (x, y) => x * y / gcd(x, y);
return arr.reduce((a, b) => helperLcm(a, b));
};
```
@ -2767,6 +2647,61 @@ const lcm = (x, y) => {
```js
lcm(12, 7); // 84
lcm([1, 3, 4], 5); // 60
```
</details>
[⬆ Back to top](#table-of-contents)
### max
Returns the maximum value out of two or more numbers/arrays.
Use `Math.max()` combined with the spread operator (`...`) to get the maximum value in the array.
```js
const max = (...arr) => Math.max(...[].concat(...arr);
```
<details>
<summary>Examples</summary>
```js
max([10, 1, 5]); // 10
```
</details>
@ -2804,6 +2739,29 @@ median([0, 10, -2, 7]); // 3.5
[⬆ Back to top](#table-of-contents)
### min
Returns the minimum value in an array.
Use `Math.min()` combined with the spread operator (`...`) to get the minimum value in the array.
```js
const min = arr => Math.min(...[].concat(...arr));
```
<details>
<summary>Examples</summary>
```js
min([10, 1, 5]); // 1
```
</details>
[⬆ Back to top](#table-of-contents)
### palindrome
Returns `true` if the given string is a palindrome, `false` otherwise.
@ -3014,6 +2972,29 @@ standardDeviation([10, 2, 38, 23, 38, 23, 21], true); // 12.29899614287479 (popu
</details>
[⬆ Back to top](#table-of-contents)
### sum
Returns the sum of an of two or more numbers/arrays.
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`.
```js
const sum = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0);
```
<details>
<summary>Examples</summary>
```js
sum([1, 2, 3, 4]); // 10
```
</details>
[⬆ Back to top](#table-of-contents)
## Media

View File

@ -107,11 +107,7 @@
<a class="sublink-1" href="#spreadover">spreadOver</a>
<h3>Array
</h3><a class="sublink-1" href="#arraygcd">arrayGcd</a>
<a class="sublink-1" href="#arraylcm">arrayLcm</a>
<a class="sublink-1" href="#arraymax">arrayMax</a>
<a class="sublink-1" href="#arraymin">arrayMin</a>
<a class="sublink-1" href="#chunk">chunk</a>
</h3><a class="sublink-1" href="#chunk">chunk</a>
<a class="sublink-1" href="#compact">compact</a>
<a class="sublink-1" href="#countoccurrences">countOccurrences</a>
<a class="sublink-1" href="#deepflatten">deepFlatten</a>
@ -189,8 +185,7 @@
</h3><a class="sublink-1" href="#negate">negate</a>
<h3>Math
</h3><a class="sublink-1" href="#arrayaverage">arrayAverage</a>
<a class="sublink-1" href="#arraysum">arraySum</a>
</h3><a class="sublink-1" href="#average">average</a>
<a class="sublink-1" href="#clampnumber">clampNumber</a>
<a class="sublink-1" href="#collatz">collatz</a>
<a class="sublink-1" href="#digitize">digitize</a>
@ -207,7 +202,9 @@
<a class="sublink-1" href="#iseven">isEven</a>
<a class="sublink-1" href="#isprime">isPrime</a>
<a class="sublink-1" href="#lcm">lcm</a>
<a class="sublink-1" href="#max">max</a>
<a class="sublink-1" href="#median">median</a>
<a class="sublink-1" href="#min">min</a>
<a class="sublink-1" href="#palindrome">palindrome</a>
<a class="sublink-1" href="#percentile">percentile</a>
<a class="sublink-1" href="#powerset">powerset</a>
@ -216,6 +213,7 @@
<a class="sublink-1" href="#randomnumberinrange">randomNumberInRange</a>
<a class="sublink-1" href="#round">round</a>
<a class="sublink-1" href="#standarddeviation">standardDeviation</a>
<a class="sublink-1" href="#sum">sum</a>
<h3>Media
</h3><a class="sublink-1" href="#speechsynthesis">speechSynthesis</a>
@ -342,44 +340,7 @@ arrayMax([1, 2, 3]); // 3
arrayMax([1, 2, 4]); // 4
</code></pre>
</div></div><br/><h2 style="text-align:center">Array</h2>
<div class="card fluid"><div class="section double-padded"><h3 id="arraygcd">arrayGcd</h3></div><div class="section double-padded">
<p>Calculates the greatest common denominator (gcd) of an array of numbers.</p>
<p>Use <code>Array.reduce()</code> and the <code>gcd</code> formula (uses recursion) to calculate the greatest common denominator of an array of numbers.</p>
<pre><code class="language-js">const arrayGcd = arr =&gt; {
const gcd = (x, y) =&gt; (!y ? x : gcd(y, x % y));
return arr.reduce((a, b) =&gt; gcd(a, b));
};
</code></pre>
<pre><code class="language-js">arrayGcd([1, 2, 3, 4, 5]); // 1
arrayGcd([4, 8, 12]); // 4
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="arraylcm">arrayLcm</h3></div><div class="section double-padded">
<p>Calculates the lowest common multiple (lcm) of an array of numbers.</p>
<p>Use <code>Array.reduce()</code> and the <code>lcm</code> formula (uses recursion) to calculate the lowest common multiple of an array of numbers.</p>
<pre><code class="language-js">const arrayLcm = arr =&gt; {
const gcd = (x, y) =&gt; (!y ? x : gcd(y, x % y));
const lcm = (x, y) =&gt; x * y / gcd(x, y);
return arr.reduce((a, b) =&gt; lcm(a, b));
};
</code></pre>
<pre><code class="language-js">arrayLcm([1, 2, 3, 4, 5]); // 60
arrayLcm([4, 8, 12]); // 24
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="arraymax">arrayMax</h3></div><div class="section double-padded">
<p>Returns the maximum value in an array.</p>
<p>Use <code>Math.max()</code> combined with the spread operator (<code>...</code>) to get the maximum value in the array.</p>
<pre><code class="language-js">const arrayMax = arr =&gt; Math.max(...arr);
</code></pre>
<pre><code class="language-js">arrayMax([10, 1, 5]); // 10
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="arraymin">arrayMin</h3></div><div class="section double-padded">
<p>Returns the minimum value in an array.</p>
<p>Use <code>Math.min()</code> combined with the spread operator (<code>...</code>) to get the minimum value in the array.</p>
<pre><code class="language-js">const arrayMin = arr =&gt; Math.min(...arr);
</code></pre>
<pre><code class="language-js">arrayMin([10, 1, 5]); // 1
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="chunk">chunk</h3></div><div class="section double-padded">
<div class="card fluid"><div class="section double-padded"><h3 id="chunk">chunk</h3></div><div class="section double-padded">
<p>Chunks an array into smaller arrays of a specified size.</p>
<p>Use <code>Array.from()</code> to create a new array, that fits the number of chunks that will be produced.
Use <code>Array.slice()</code> to map each element of the new array to a chunk the length of <code>size</code>.
@ -1072,19 +1033,12 @@ runPromisesInSeries([() =&gt; delay(1000), () =&gt; delay(2000)]); // //executes
negate(isOdd)(1); // false
</code></pre>
</div></div><br/><h2 style="text-align:center">Math</h2>
<div class="card fluid"><div class="section double-padded"><h3 id="arrayaverage">arrayAverage</h3></div><div class="section double-padded">
<p>Returns the average of an array of numbers.</p>
<div class="card fluid"><div class="section double-padded"><h3 id="average">average</h3></div><div class="section double-padded">
<p>Returns the average of an of two or more numbers/arrays.</p>
<p>Use <code>Array.reduce()</code> to add each value to an accumulator, initialized with a value of <code>0</code>, divide by the <code>length</code> of the array.</p>
<pre><code class="language-js">const arrayAverage = arr =&gt; arr.reduce((acc, val) =&gt; acc + val, 0) / arr.length;
<pre><code class="language-js">const average = (...arr) =&gt; [].concat(...arr).reduce((acc, val) =&gt; acc + val, 0) / arr.length;
</code></pre>
<pre><code class="language-js">arrayAverage([1, 2, 3]); // 2
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="arraysum">arraySum</h3></div><div class="section double-padded">
<p>Returns the sum of an array of numbers.</p>
<p>Use <code>Array.reduce()</code> to add each value to an accumulator, initialized with a value of <code>0</code>.</p>
<pre><code class="language-js">const arraySum = arr =&gt; arr.reduce((acc, val) =&gt; acc + val, 0);
</code></pre>
<pre><code class="language-js">arraySum([1, 2, 3, 4]); // 10
<pre><code class="language-js">average([1, 2, 3]); // 2
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="clampnumber">clampNumber</h3></div><div class="section double-padded">
<p>Clamps <code>num</code> within the inclusive range specified by the boundary values <code>a</code> and <code>b</code>.</p>
@ -1170,11 +1124,15 @@ Uses a mathematical formula to calculate the length of the array required.</p>
<pre><code class="language-js">fibonacciCountUntilNum(10); // 7
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="gcd">gcd</h3></div><div class="section double-padded">
<p>Calculates the greatest common divisor between two numbers.</p>
<p>Use recursion.
<p>Calculates the greatest common divisor between two or more numbers/arrays.</p>
<p>The <code>helperGcd</code>function uses recursion.
Base case is when <code>y</code> equals <code>0</code>. In this case, return <code>x</code>.
Otherwise, return the GCD of <code>y</code> and the remainder of the division <code>x/y</code>.</p>
<pre><code class="language-js">const gcd = (x, y) =&gt; (!y ? x : gcd(y, x % y));
<pre><code class="language-js">const gcd = (...arr) =&gt; {
let data = [].concat(...arr);
const helperGcd = (x, y) =&gt; (!y ? x : gcd(y, x % y));
return data.reduce((a, b) =&gt; helperGcd(a, b));
};
</code></pre>
<pre><code class="language-js">gcd(8, 36); // 4
</code></pre>
@ -1241,15 +1199,56 @@ Return <code>false</code> if any of them divides the given number, else return <
isPrime(12); // false
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="lcm">lcm</h3></div><div class="section double-padded">
<p>Returns the least common multiple of two numbers.</p>
<p>Returns the least common multiple of two or numbers/arrays.</p>
<p>Use the greatest common divisor (GCD) formula and <code>Math.abs()</code> to determine the least common multiple.
The GCD formula uses recursion.</p>
<pre><code class="language-js">const lcm = (x, y) =&gt; {
<pre><code class="language-js">const lcm = (...arr) =&gt; {
let data = [].concat(...arr);
const gcd = (x, y) =&gt; (!y ? x : gcd(y, x % y));
return Math.abs(x * y) / gcd(x, y);
const helperLcm = (x, y) =&gt; x * y / gcd(x, y);
return arr.reduce((a, b) =&gt; helperLcm(a, b));
};
</code></pre>
<pre><code class="language-js">lcm(12, 7); // 84
lcm([1, 3, 4], 5); // 60
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="max">max</h3></div><div class="section double-padded">
<p>Returns the maximum value out of two or more numbers/arrays.</p>
<p>Use <code>Math.max()</code> combined with the spread operator (<code>...</code>) to get the maximum value in the array.</p>
<pre><code class="language-js">
const max = (...arr) =&gt; Math.max(...[].concat(...arr);
</code></pre>
<pre><code class="language-js">max([10, 1, 5]); // 10
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="median">median</h3></div><div class="section double-padded">
<p>Returns the median of an array of numbers.</p>
@ -1264,6 +1263,13 @@ Return the number at the midpoint if <code>length</code> is odd, otherwise the a
<pre><code class="language-js">median([5, 6, 50, 1, -5]); // 5
median([0, 10, -2, 7]); // 3.5
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="min">min</h3></div><div class="section double-padded">
<p>Returns the minimum value in an array.</p>
<p>Use <code>Math.min()</code> combined with the spread operator (<code>...</code>) to get the minimum value in the array.</p>
<pre><code class="language-js">const min = arr =&gt; Math.min(...[].concat(...arr));
</code></pre>
<pre><code class="language-js">min([10, 1, 5]); // 1
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="palindrome">palindrome</h3></div><div class="section double-padded">
<p>Returns <code>true</code> if the given string is a palindrome, <code>false</code> otherwise.</p>
<p>Convert string <code>toLowerCase()</code> and use <code>replace()</code> to remove non-alphanumeric characters from it.
@ -1349,6 +1355,13 @@ You can omit the second argument to get the sample standard deviation or set it
<pre><code class="language-js">standardDeviation([10, 2, 38, 23, 38, 23, 21]); // 13.284434142114991 (sample)
standardDeviation([10, 2, 38, 23, 38, 23, 21], true); // 12.29899614287479 (population)
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="sum">sum</h3></div><div class="section double-padded">
<p>Returns the sum of an of two or more numbers/arrays.</p>
<p>Use <code>Array.reduce()</code> to add each value to an accumulator, initialized with a value of <code>0</code>.</p>
<pre><code class="language-js">const sum = (...arr) =&gt; [].concat(...arr).reduce((acc, val) =&gt; acc + val, 0);
</code></pre>
<pre><code class="language-js">sum([1, 2, 3, 4]); // 10
</code></pre>
</div></div><br/><h2 style="text-align:center">Media</h2>
<div class="card fluid"><div class="section double-padded"><h3 id="speechsynthesis">speechSynthesis</h3></div><div class="section double-padded">
<p>Performs speech synthesis (experimental).</p>

View File

@ -73,7 +73,7 @@ try {
if(capitalize(tag, true)=='Uncategorized') {
uncategorizedOutput +=`## _${capitalize(tag, true)}_\n`;
for(let taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag))
uncategorizedOutput += `\n${snippets[taggedSnippet[0]+'.md']+'\n[⬆ back to top](#table-of-contents)\n'}`;
uncategorizedOutput += `\n${snippets[taggedSnippet[0]+'.md']+'\n<br>[⬆ back to top](#table-of-contents)\n\n'}`;
} else {
output +=`## ${capitalize(tag, true)}\n`;
for(let taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)){

View File

@ -11,7 +11,7 @@ const gcd = (...arr) => {
let data = [].concat(...arr);
const helperGcd = (x, y) => (!y ? x : gcd(y, x % y));
return data.reduce((a, b) => helperGcd(a, b));
}
};
```
```js

View File

@ -10,11 +10,11 @@ const lcm = (...arr) => {
let data = [].concat(...arr);
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
const helperLcm = (x, y) => x * y / gcd(x, y);
return arr.reduce((a, b) => helperLcm(a, b))
}
return arr.reduce((a, b) => helperLcm(a, b));
};
```
```js
lcm(12, 7); // 84
lcm([1,3,4],5); // 60
lcm([1, 3, 4], 5); // 60
```

View File

@ -5,6 +5,37 @@ Returns the maximum value out of two or more numbers/arrays.
Use `Math.max()` combined with the spread operator (`...`) to get the maximum value in the array.
```js
const max = (...arr) => Math.max(...[].concat(...arr);
```

View File

@ -1,11 +1,6 @@
anagrams:string
arrayAverage:math
arrayGcd:array
arrayLcm:array
arrayMax:array
arrayMin:array
arraySum:math
arrayToHtmlList:browser
average:math
bottomVisible:browser
call:adapter
capitalize:string
@ -81,7 +76,9 @@ JSONToFile:node
last:array
lcm:math
mapObject:array
max:math
median:math
min:math
negate:logic
nthElement:array
objectFromPairs:object
@ -123,6 +120,7 @@ sortCharactersInString:string
speechSynthesis:media
spreadOver:adapter
standardDeviation:math
sum:math
symmetricDifference:array
tail:array
take:array