diff --git a/docs/index.html b/docs/index.html index ecb78bb7a..a10547ab1 100644 --- a/docs/index.html +++ b/docs/index.html @@ -22,6 +22,26 @@ var remove = false, childs = Array.from(node.parentElement.parentElement.children), toRemove = childs[0]; Array.from(node.parentElement.parentElement.children).forEach(x => x.tagName == 'H3' ? (toRemove.style.display = (remove ? 'none' : ''), toRemove = x, remove = true) : (x.style.display == '' ? remove = false : remove=remove)); } + + const snippets = document.querySelectorAll("pre"); + snippets.forEach(element => { + const button = document.createElement("button"); + button.innerHTML = "Copy to clipboard"; + element.parentElement.appendChild(button); + + button.addEventListener ("click", function() { + //The following regex removes all the comments from the snippet + const text = element.textContent.replace(/\/\*(.|[\r\n])*?\*\//g, '').replace(/\/\/.*/gm, ''); + // Apparently you can't copy a variable to clipboard so you need to create text input element, + // give it a value, copy it and then remove it from DOM. + const textArea = document.createElement("textarea"); + textArea.value = text; + document.body.appendChild(textArea); + textArea.select(); + document.execCommand("Copy"); + document.body.removeChild(textArea); + }); +});
@@ -273,7 +293,7 @@ arrayMax([1,2,4]) // -> 4Use Array.reduce() and the lcm formula (uses recursion) to calculate the lowest common multiple of an array of numbers.
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
@@ -417,7 +437,7 @@ Use Array.reduce() to create an object, where the keys are produced
Initializes an array containing the numbers in the specified range where start and end are inclusive.
Use Array((end + 1) - start) to create an array of the desired length, Array.map() to fill with the desired values in a range.
You can omit start to use a default value of 0.
-const initializeArrayWithRange = (end, start = 0) =>
+const initializeArrayWithRange = (end, start = 0) =>
Array.from({ length: (end + 1) - start }).map((v, i) => i + start);
// initializeArrayWithRange(5) -> [0,1,2,3,4,5]
// initializeArrayWithRange(7, 3) -> [3,4,5,6,7]
@@ -444,7 +464,7 @@ You can omit value to use a default value of 0.
mapObject
Maps the values of an array to an object using a function, where the key-value pairs consist of the original value as the key and the mapped value.
Use an anonymous inner function scope to declare an undefined memory space, using closures to store a return value. Use a new Array to store the array with a map of the function over its data set and a comma operator to return a second step, without needing to move from one context to another (due to closures and order of operations).
-const mapObject = (arr, fn) =>
+const mapObject = (arr, fn) =>
(a => (a = [arr, arr.map(fn)], a[0].reduce( (acc,val,ind) => (acc[val] = a[1][ind], acc), {}) )) ( );
/*
const squareIt = arr => mapObject(arr, a => a*a)
@@ -475,7 +495,7 @@ Use Array.length = 0 to mutate the passed in an array by resetting
const pull = (arr, ...args) => {
let argState = Array.isArray(args[0]) ? args[0] : args;
let pulled = arr.filter((v, i) => !argState.includes(v));
- arr.length = 0;
+ arr.length = 0;
pulled.forEach(v => arr.push(v));
};
@@ -496,7 +516,7 @@ Use Array.push() to keep track of pulled values
let removed = [];
let pulled = arr.map((v, i) => pullArr.includes(i) ? removed.push(v) : v)
.filter((v, i) => !pullArr.includes(i))
- arr.length = 0;
+ arr.length = 0;
pulled.forEach(v => arr.push(v));
return removed;
}
@@ -513,7 +533,7 @@ Use Array.push() to keep track of pulled values
Use Array.length = 0 to mutate the passed in an array by resetting it's length to zero and Array.push() to re-populate it with only the pulled values.
Use Array.push() to keep track of pulled values
const pullAtValue = (arr, pullArr) => {
- let removed = [],
+ let removed = [],
pushToRemove = arr.forEach((v, i) => pullArr.includes(v) ? removed.push(v) : v),
mutateTo = arr.filter((v, i) => !pullArr.includes(v));
arr.length = 0;
@@ -825,7 +845,7 @@ If num falls within the range, return num.
Otherwise, return the nearest number in the range.
const clampNumber = (num, lower, upper) => {
if(lower > upper) upper = [lower, lower = upper][0];
- return (num>=lower && num<=upper) ? num : ((num < lower) ? lower : upper)
+ return (num>=lower && num<=upper) ? num : ((num < lower) ? lower : upper)
}
// clampNumber(2, 3, 5) -> 3
// clampNumber(1, -1, -5) -> -1
@@ -920,7 +940,7 @@ If the second parameter, end, is not specified, the range is consid
isArmstrongNumber
Checks if the given number is an Armstrong number or not.
Convert the given number into an array of digits. Use Math.pow() to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return true otherwise false.
-const isArmstrongNumber = digits =>
+const isArmstrongNumber = digits =>
( arr => arr.reduce( ( a, d ) => a + Math.pow( parseInt( d ), arr.length ), 0 ) == digits ? true : false )( ( digits+'' ).split( '' ) );
// isArmstrongNumber(1634) -> true
// isArmstrongNumber(371) -> true
@@ -1000,13 +1020,13 @@ Then, split('') into individual characters, reverse(),
Generates primes up to a given number, using the Sieve of Eratosthenes.
Generate an array from 2 to the given number. Use Array.filter() to filter out the values divisible by any number from 2 to the square root of the provided number.
const primes = num => {
- let arr = Array.from({length:num-1}).map((x,i)=> i+2),
+ let arr = Array.from({length:num-1}).map((x,i)=> i+2),
sqroot = Math.floor(Math.sqrt(num)),
numsTillSqroot = Array.from({length:sqroot-1}).map((x,i)=> i+2);
numsTillSqroot.forEach(x => arr = arr.filter(y => ((y%x)!==0)||(y==x)));
- return arr;
+ return arr;
}
-// primes(10) -> [2,3,5,7]
+// primes(10) -> [2,3,5,7]
randomIntegerInRange
Returns a random integer in the specified range.
diff --git a/static-parts/index-start.html b/static-parts/index-start.html
index b81e02d7a..cb8bab170 100644
--- a/static-parts/index-start.html
+++ b/static-parts/index-start.html
@@ -23,8 +23,8 @@
Array.from(node.parentElement.parentElement.children).forEach(x => x.tagName == 'H3' ? (toRemove.style.display = (remove ? 'none' : ''), toRemove = x, remove = true) : (x.style.display == '' ? remove = false : remove=remove));
}
- const pres = document.querySelectorAll("pre");
- pres.forEach(element => {
+ const snippets = document.querySelectorAll("pre");
+ snippets.forEach(element => {
const button = document.createElement("button");
button.innerHTML = "Copy to clipboard";
element.parentElement.appendChild(button);
@@ -32,8 +32,8 @@
button.addEventListener ("click", function() {
//The following regex removes all the comments from the snippet
const text = element.textContent.replace(/\/\*(.|[\r\n])*?\*\//g, '').replace(/\/\/.*/gm, '');
- // Apparently you can copy variable to clipboard so you need to create text input element,
- // give it a value, copy it and then remove it
+ // Apparently you can't copy a variable to clipboard so you need to create text input element,
+ // give it a value, copy it and then remove it from DOM.
const textArea = document.createElement("textarea");
textArea.value = text;
document.body.appendChild(textArea);