Merge branch 'web-searchbar' of https://github.com/Pl4gue/30-seconds-of-code
This commit is contained in:
48
README.md
48
README.md
@ -99,6 +99,8 @@
|
||||
* [`distance`](#distance)
|
||||
* [`factorial`](#factorial)
|
||||
* [`fibonacci`](#fibonacci)
|
||||
* [`fibonacciCountUntilNum`](#fibonaccicountuntilnum)
|
||||
* [`fibonacciUntilNum`](#fibonacciuntilnum)
|
||||
* [`gcd`](#gcd)
|
||||
* [`hammingDistance`](#hammingdistance)
|
||||
* [`inRange`](#inrange)
|
||||
@ -180,7 +182,7 @@ Promise.resolve( [ 1, 2, 3 ] ).then( call('map', x => 2 * x ) ).then( console.lo
|
||||
const map = call.bind(null, 'map')
|
||||
Promise.resolve( [ 1, 2, 3 ] ).then( map( x => 2 * x ) ).then( console.log ) //[ 2, 4, 6 ]
|
||||
*/
|
||||
```
|
||||
```
|
||||
|
||||
[⬆ back to top](#table-of-contents)
|
||||
|
||||
@ -199,7 +201,7 @@ let p2 = Promise.resolve(2)
|
||||
let p3 = new Promise((resolve) => setTimeout(resolve,2000,3))
|
||||
Pall(p1, p2, p3).then(console.log)
|
||||
*/
|
||||
```
|
||||
```
|
||||
|
||||
[⬆ back to top](#table-of-contents)
|
||||
|
||||
@ -220,7 +222,7 @@ mergePerson(b) // == b
|
||||
b = {}
|
||||
Object.assign(b, a) // == b
|
||||
*/
|
||||
```
|
||||
```
|
||||
|
||||
[⬆ back to top](#table-of-contents)
|
||||
|
||||
@ -259,7 +261,7 @@ const arrayMax = spreadOver(Math.max)
|
||||
arrayMax([1,2,3]) // -> 3
|
||||
arrayMax([1,2,4]) // -> 4
|
||||
*/
|
||||
```
|
||||
```
|
||||
|
||||
[⬆ back to top](#table-of-contents)
|
||||
## Array
|
||||
@ -290,7 +292,7 @@ Use `Array.reduce()` and the `lcm` formula (uses recursion) to calculate the low
|
||||
```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
|
||||
@ -483,10 +485,10 @@ const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexO
|
||||
|
||||
Flattens an array.
|
||||
|
||||
Use `Array.reduce()` to get all elements inside the array and `concat()` to flatten them.
|
||||
Use a new array and concatenate it with the spread input array causing a shallow denesting of any contained arrays.
|
||||
|
||||
```js
|
||||
const flatten = arr => arr.reduce((a, v) => a.concat(v), []);
|
||||
const flatten = arr => [ ].concat( ...arr );
|
||||
// flatten([1,[2],3,4]) -> [1,2,3,4]
|
||||
```
|
||||
|
||||
@ -1368,6 +1370,38 @@ const fibonacci = n =>
|
||||
|
||||
[⬆ back to top](#table-of-contents)
|
||||
|
||||
### fibonacciCountUntilNum
|
||||
|
||||
Returns the number of fibonnacci numbers up to `num`(`0` and `num` inclusive).
|
||||
|
||||
Use a mathematical formula to calculate the number of fibonacci numbers until `num`.
|
||||
|
||||
```js
|
||||
const fibonacciCountUntilNum = num =>
|
||||
Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2));
|
||||
// fibonacciCountUntilNum(10) -> 7
|
||||
```
|
||||
|
||||
[⬆ back to top](#table-of-contents)
|
||||
|
||||
### fibonacciUntilNum
|
||||
|
||||
Generates an array, containing the Fibonacci sequence, up until the nth term.
|
||||
|
||||
Create an empty array of the specific length, initializing the first two values (`0` and `1`).
|
||||
Use `Array.reduce()` 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.
|
||||
|
||||
```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]
|
||||
```
|
||||
|
||||
[⬆ back to top](#table-of-contents)
|
||||
|
||||
### gcd
|
||||
|
||||
Calculates the greatest common divisor between two numbers.
|
||||
|
||||
1697
dist/flavor.css
vendored
Normal file
1697
dist/flavor.css
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1
dist/flavor.min.css
vendored
Normal file
1
dist/flavor.min.css
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -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);}
|
||||
|
||||
@ -10,6 +10,7 @@ var snippetsPath = './snippets';
|
||||
// Read files, lint each one individually and update
|
||||
try {
|
||||
let snippetFilenames = fs.readdirSync(snippetsPath);
|
||||
let jobCounter = 0;
|
||||
snippetFilenames.sort((a, b) => {
|
||||
a = a.toLowerCase();
|
||||
b = b.toLowerCase();
|
||||
@ -25,10 +26,14 @@ try {
|
||||
// Synchronously read data from the snippet, get the code, write it to a temporary file
|
||||
let snippetData = fs.readFileSync(path.join(snippetsPath,snippet),'utf8');
|
||||
let originalCode = snippetData.slice(snippetData.indexOf('```js')+5,snippetData.lastIndexOf('```'));
|
||||
while(jobCounter >= 20){
|
||||
setTimeout(()=>{},5000);
|
||||
}
|
||||
fs.writeFileSync(`${snippet}.temp.js`,`${originalCode}`);
|
||||
// Run semistandard asynchronously (only way this manages to run), get linted code
|
||||
// and write back to the original snippet file. Remove temporary file
|
||||
cp.exec(`semistandard "${snippet}.temp.js" --fix`,{},(error, stdOut, stdErr) => {
|
||||
jobCounter += 1;
|
||||
let lintedCode = fs.readFileSync(`${snippet}.temp.js`,'utf8');
|
||||
fs.writeFile(path.join(snippetsPath,snippet), `${snippetData.slice(0, snippetData.indexOf('```js')+5)+lintedCode+'```\n'}`);
|
||||
fs.unlink(`${snippet}.temp.js`);
|
||||
@ -36,6 +41,7 @@ try {
|
||||
console.log(`${chalk.green('SUCCESS!')} Linted snippet: ${snippet}`);
|
||||
// Log the time taken for the file
|
||||
console.timeEnd(`Linter (${snippet})`);
|
||||
jobCounter -= 1;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@ Use `Array.reduce()` and the `lcm` formula (uses recursion) to calculate the low
|
||||
```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
|
||||
|
||||
@ -11,4 +11,4 @@ Promise.resolve( [ 1, 2, 3 ] ).then( call('map', x => 2 * x ) ).then( console.lo
|
||||
const map = call.bind(null, 'map')
|
||||
Promise.resolve( [ 1, 2, 3 ] ).then( map( x => 2 * x ) ).then( console.log ) //[ 2, 4, 6 ]
|
||||
*/
|
||||
```
|
||||
```
|
||||
|
||||
@ -13,4 +13,4 @@ let p2 = Promise.resolve(2)
|
||||
let p3 = new Promise((resolve) => setTimeout(resolve,2000,3))
|
||||
Pall(p1, p2, p3).then(console.log)
|
||||
*/
|
||||
```
|
||||
```
|
||||
|
||||
11
snippets/fibonacciCountUntilNum.md
Normal file
11
snippets/fibonacciCountUntilNum.md
Normal file
@ -0,0 +1,11 @@
|
||||
### fibonacciCountUntilNum
|
||||
|
||||
Returns the number of fibonnacci numbers up to `num`(`0` and `num` inclusive).
|
||||
|
||||
Use a mathematical formula to calculate the number of fibonacci numbers until `num`.
|
||||
|
||||
```js
|
||||
const fibonacciCountUntilNum = num =>
|
||||
Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2));
|
||||
// fibonacciCountUntilNum(10) -> 7
|
||||
```
|
||||
15
snippets/fibonacciUntilNum.md
Normal file
15
snippets/fibonacciUntilNum.md
Normal file
@ -0,0 +1,15 @@
|
||||
### fibonacciUntilNum
|
||||
|
||||
Generates an array, containing the Fibonacci sequence, up until the nth term.
|
||||
|
||||
Create an empty array of the specific length, initializing the first two values (`0` and `1`).
|
||||
Use `Array.reduce()` 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.
|
||||
|
||||
```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]
|
||||
```
|
||||
@ -2,9 +2,9 @@
|
||||
|
||||
Flattens an array.
|
||||
|
||||
Use `Array.reduce()` to get all elements inside the array and `concat()` to flatten them.
|
||||
Use a new array and concatenate it with the spread input array causing a shallow denesting of any contained arrays.
|
||||
|
||||
```js
|
||||
const flatten = arr => arr.reduce((a, v) => a.concat(v), []);
|
||||
const flatten = arr => [ ].concat( ...arr );
|
||||
// flatten([1,[2],3,4]) -> [1,2,3,4]
|
||||
```
|
||||
|
||||
@ -15,4 +15,4 @@ mergePerson(b) // == b
|
||||
b = {}
|
||||
Object.assign(b, a) // == b
|
||||
*/
|
||||
```
|
||||
```
|
||||
|
||||
@ -11,4 +11,4 @@ const arrayMax = spreadOver(Math.max)
|
||||
arrayMax([1,2,3]) // -> 3
|
||||
arrayMax([1,2,4]) // -> 4
|
||||
*/
|
||||
```
|
||||
```
|
||||
|
||||
@ -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>
|
||||
|
||||
@ -38,6 +38,8 @@ everyNth:array
|
||||
extendHex:utility
|
||||
factorial:math
|
||||
fibonacci:math
|
||||
fibonacciCountUntilNum:math
|
||||
fibonacciUntilNum:math
|
||||
filterNonUnique:array
|
||||
flatten:array
|
||||
flattenDepth:array
|
||||
|
||||
Reference in New Issue
Block a user