Merge branch 'master' of https://github.com/Chalarangelo/30-seconds-of-code into web-searchbar

This commit is contained in:
Pl4gue
2017-12-23 15:50:16 +01:00
16 changed files with 1807 additions and 19 deletions

View File

@ -134,6 +134,8 @@
<a class="sublink-1" href="#distance">distance</a>
<a class="sublink-1" href="#factorial">factorial</a>
<a class="sublink-1" href="#fibonacci">fibonacci</a>
<a class="sublink-1" href="#fibonaccicountuntilnum">fibonacciCountUntilNum</a>
<a class="sublink-1" href="#fibonacciuntilnum">fibonacciUntilNum</a>
<a class="sublink-1" href="#gcd">gcd</a>
<a class="sublink-1" href="#hammingdistance">hammingDistance</a>
<a class="sublink-1" href="#inrange">inRange</a>
@ -277,7 +279,7 @@ arrayMax([1,2,4]) // -&gt; 4
<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)
const lcm = (x, y) =&gt; (x*y)/gcd(x, y);
return arr.reduce((a,b) =&gt; lcm(a,b));
}
// arrayLcm([1,2,3,4,5]) -&gt; 60
@ -374,8 +376,8 @@ Returns the remaining elements.</p>
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="flatten">flatten</h3></div><div class="section double-padded">
<p>Flattens an array.</p>
<p>Use <code>Array.reduce()</code> to get all elements inside the array and <code>concat()</code> to flatten them.</p>
<pre><code class="language-js">const flatten = arr =&gt; arr.reduce((a, v) =&gt; a.concat(v), []);
<p>Use a new array and concatenate it with the spread input array causing a shallow denesting of any contained arrays.</p>
<pre><code class="language-js">const flatten = arr =&gt; [ ].concat( ...arr );
// flatten([1,[2],3,4]) -&gt; [1,2,3,4]
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="flattendepth">flattenDepth</h3></div><div class="section double-padded">
@ -874,6 +876,24 @@ Use <code>Array.reduce()</code> to add values into the array, using the sum of t
Array.from({ length: n}).reduce((acc, val, i) =&gt; acc.concat(i &gt; 1 ? acc[i - 1] + acc[i - 2] : i), []);
// fibonacci(5) -&gt; [0,1,1,2,3]
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="fibonaccicountuntilnum">fibonacciCountUntilNum</h3></div><div class="section double-padded">
<p>Returns the number of fibonnacci numbers up to <code>num</code>(<code>0</code> and <code>num</code> inclusive).</p>
<p>Use a mathematical formula to calculate the number of fibonacci numbers until <code>num</code>.</p>
<pre><code class="language-js">const fibonacciCountUntilNum = num =&gt;
Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2));
// fibonacciCountUntilNum(10) -&gt; 7
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="fibonacciuntilnum">fibonacciUntilNum</h3></div><div class="section double-padded">
<p>Generates an array, containing the Fibonacci sequence, up until the nth term.</p>
<p>Create an empty array of the specific length, initializing the first two values (<code>0</code> and <code>1</code>).
Use <code>Array.reduce()</code> to add values into the array, using the sum of the last two values, except for the first two.
Uses a mathematical formula to calculate the length of the array required.</p>
<pre><code class="language-js">const fibonacciUntilNum = num =&gt; {
let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2));
return Array.from({ length: n}).reduce((acc, val, i) =&gt; acc.concat(i &gt; 1 ? acc[i - 1] + acc[i - 2] : i), []);
}
// fibonacciUntilNum(15) -&gt; [0,1,1,2,3,5,8,13]
</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.