From 5c33ce5a1e726e5a93e2e257d1c93ac6d5814a37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Sat, 23 Dec 2017 15:19:41 +0100 Subject: [PATCH 1/8] add buttons and eventListeners --- docs/bundle.js | 10 ++++++++++ docs/index.html | 2 ++ static-parts/index-end.html | 1 + static-parts/index-start.html | 1 + 4 files changed, 14 insertions(+) create mode 100644 docs/bundle.js diff --git a/docs/bundle.js b/docs/bundle.js new file mode 100644 index 000000000..eb3d70564 --- /dev/null +++ b/docs/bundle.js @@ -0,0 +1,10 @@ +var s = document.querySelectorAll("pre"); +s.forEach(element => { + var button = document.createElement("button"); + button.innerHTML = "Copy to clipboard"; + element.parentElement.appendChild(button); + + button.addEventListener ("click", function() { + console.log(element.textContent); + }); +}); \ No newline at end of file diff --git a/docs/index.html b/docs/index.html index ee07a9add..ecb78bb7a 100644 --- a/docs/index.html +++ b/docs/index.html @@ -14,6 +14,7 @@ + + diff --git a/static-parts/index-end.html b/static-parts/index-end.html index 2dd2aae7e..1b89d551a 100644 --- a/static-parts/index-end.html +++ b/static-parts/index-end.html @@ -5,5 +5,6 @@ + diff --git a/static-parts/index-start.html b/static-parts/index-start.html index f6c71b064..d1e09b45c 100644 --- a/static-parts/index-start.html +++ b/static-parts/index-start.html @@ -14,6 +14,7 @@ + From c4d4691c2ce09eaa8a22d7d1d4af541fed19026c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Sat, 23 Dec 2017 16:11:17 +0100 Subject: [PATCH 4/8] build webber --- docs/index.html | 42 ++++++++++++++++++++++++++--------- static-parts/index-start.html | 8 +++---- 2 files changed, 35 insertions(+), 15 deletions(-) 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]) // -> 4

Use 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); From a536f2a67d846ffd795624b5009bd40463629f48 Mon Sep 17 00:00:00 2001 From: Pl4gue Date: Sat, 23 Dec 2017 17:34:12 +0100 Subject: [PATCH 5/8] Delete unnecessary js file --- docs/bundle.js | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 docs/bundle.js diff --git a/docs/bundle.js b/docs/bundle.js deleted file mode 100644 index 009dec509..000000000 --- a/docs/bundle.js +++ /dev/null @@ -1,20 +0,0 @@ -var pres = document.querySelectorAll("pre"); -pres.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 copy variable to clipboard so you need to create text input element, - // give it a value, copy it and then remove it - const textArea = document.createElement("textarea"); - textArea.value = text; - document.body.appendChild(textArea); - textArea.select(); - document.execCommand("Copy"); - document.body.removeChild(textArea); - console.log(`copied: ${text}`); - }); -}); \ No newline at end of file From c12f2c7c7aeebe8d271aedd31ec152627dec708b Mon Sep 17 00:00:00 2001 From: Pl4gue Date: Sat, 23 Dec 2017 17:34:39 +0100 Subject: [PATCH 6/8] Delete unnecessary js file --- static-parts/index-end.html | 1 - 1 file changed, 1 deletion(-) diff --git a/static-parts/index-end.html b/static-parts/index-end.html index 1b89d551a..2dd2aae7e 100644 --- a/static-parts/index-end.html +++ b/static-parts/index-end.html @@ -5,6 +5,5 @@
- From 3ee84679c7623d8403f507267de381da8b8af083 Mon Sep 17 00:00:00 2001 From: Pl4gue Date: Sat, 23 Dec 2017 17:34:49 +0100 Subject: [PATCH 7/8] Fix trailing empty lines --- static-parts/index-start.html | 45 ++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/static-parts/index-start.html b/static-parts/index-start.html index 1da0a0a70..3a926ab47 100644 --- a/static-parts/index-start.html +++ b/static-parts/index-start.html @@ -14,11 +14,10 @@ - - +

 30 seconds of code From 36c4af2cae820e990f94c34e3c9aa1f9870c1bf0 Mon Sep 17 00:00:00 2001 From: Pl4gue Date: Sat, 23 Dec 2017 17:37:47 +0100 Subject: [PATCH 8/8] rebuild --- docs/index.html | 68 ++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/docs/index.html b/docs/index.html index 12e0b5d12..07111b089 100644 --- a/docs/index.html +++ b/docs/index.html @@ -14,11 +14,10 @@ - - +

 30 seconds of code @@ -304,7 +305,7 @@ arrayMax([1,2,4]) // -> 4

Use 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
@@ -448,7 +449,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]
@@ -475,7 +476,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)
@@ -506,7 +507,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));
 };
 
@@ -527,7 +528,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; } @@ -544,7 +545,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;
@@ -856,7 +857,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
@@ -951,7 +952,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
@@ -1031,13 +1032,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.

@@ -1432,7 +1433,6 @@ Use Number() to check if the coercion holds.

-