Travis build: 483
This commit is contained in:
@ -107,9 +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>
|
||||
</h3><a class="sublink-1" href="#arraymax">arrayMax</a>
|
||||
<a class="sublink-1" href="#arraymin">arrayMin</a>
|
||||
<a class="sublink-1" href="#chunk">chunk</a>
|
||||
<a class="sublink-1" href="#compact">compact</a>
|
||||
@ -342,30 +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 => {
|
||||
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
|
||||
return arr.reduce((a, b) => 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 => {
|
||||
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));
|
||||
};
|
||||
</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">
|
||||
<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 => Math.max(...arr);
|
||||
@ -1170,11 +1145,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) => (!y ? x : gcd(y, x % y));
|
||||
<pre><code class="language-js">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));
|
||||
};
|
||||
</code></pre>
|
||||
<pre><code class="language-js">gcd(8, 36); // 4
|
||||
</code></pre>
|
||||
@ -1241,15 +1220,18 @@ 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) => {
|
||||
<pre><code class="language-js">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));
|
||||
};
|
||||
</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="median">median</h3></div><div class="section double-padded">
|
||||
<p>Returns the median of an array of numbers.</p>
|
||||
|
||||
Reference in New Issue
Block a user