Merge branch 'web-searchbar' of https://github.com/Pl4gue/30-seconds-of-code
This commit is contained in:
@ -17,16 +17,20 @@
|
||||
</head>
|
||||
<script>
|
||||
const search = (node) => {
|
||||
// Hide non-query-matching snippets
|
||||
Array.from(node.parentElement.parentElement.getElementsByTagName('a')).forEach(x => {
|
||||
x.style.display = x.getAttribute("href").toUpperCase().indexOf(node.value.toUpperCase()) + 1 ? '' : 'none'
|
||||
});
|
||||
var remove = false, childs = Array.from(node.parentElement.parentElement.children), toRemove = childs[0];
|
||||
Array.from(node.parentElement.parentElement.children).forEach((x, idx) => {
|
||||
idx === Array.from(node.parentElement.parentElement.children).length -1 ?
|
||||
toRemove.style.display = (remove ? 'none' : '') : x.tagName == 'H3' ?
|
||||
(toRemove.style.display = (remove ? 'none' : ''), toRemove = x, remove = true) :
|
||||
(x.style.display == '' ? remove = false : remove=remove)
|
||||
});
|
||||
Array.from( node.parentElement.parentElement.children )
|
||||
// Filter out the hidden links
|
||||
.filter( x => !( x.tagName == 'A' && x.style.display == 'none' ) )
|
||||
// set the display for each element based on if it's a H3
|
||||
// If it's the last element and an H3, hide it
|
||||
// Otherwise if it's and H3 and the next element is an H3, hide it
|
||||
// Otherwise display it
|
||||
.forEach( ( element, index, source) => {
|
||||
element.style.display = (element.tagName == 'H3' && index + 1 == source.length ? 'none' : element.tagName == 'H3' && source[index + 1].tagName == 'H3' ? 'none' : '')
|
||||
})
|
||||
}
|
||||
</script>
|
||||
<body>
|
||||
@ -134,6 +138,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 +283,7 @@ arrayMax([1,2,4]) // -> 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 =>{
|
||||
const gcd = (x, y) => !y ? x : gcd(y, x % y);
|
||||
const lcm = (x, y) => (x*y)/gcd(x, y)
|
||||
const lcm = (x, y) => (x*y)/gcd(x, y);
|
||||
return arr.reduce((a,b) => lcm(a,b));
|
||||
}
|
||||
// arrayLcm([1,2,3,4,5]) -> 60
|
||||
@ -374,8 +380,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 => arr.reduce((a, v) => 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 => [ ].concat( ...arr );
|
||||
// flatten([1,[2],3,4]) -> [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 +880,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) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []);
|
||||
// fibonacci(5) -> [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 =>
|
||||
Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2));
|
||||
// fibonacciCountUntilNum(10) -> 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 => {
|
||||
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) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []);
|
||||
}
|
||||
// fibonacciUntilNum(15) -> [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.
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -110,7 +110,9 @@ $mark-tag-border-radius: 1em;
|
||||
// Website-specific styles
|
||||
|
||||
html, * { font-family: 'Poppins', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Helvetica, sans-serif; }
|
||||
code, pre, kbd, code *, pre *, kbd * { font-family: 'Inconsolata', Menlo, Consolas, monospace; }
|
||||
code, pre, kbd, code *, pre *, kbd *, code[class*="language-"], pre[class*="language-"] {
|
||||
font-family: 'Inconsolata', Menlo, Consolas, monospace !important;
|
||||
}
|
||||
code, kbd { font-size: 1em; }
|
||||
code { transform: scale(1); } /* Deals with the issue described in #243 */
|
||||
pre { font-size: 1rem; border: 0.0625rem solid var(--secondary-border-color); border-radius: var(--universal-border-radius);}
|
||||
|
||||
Reference in New Issue
Block a user