From fcbb5c1a91af67af9ef991c68c0fe1c962d1d4c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Sat, 7 Apr 2018 11:25:03 +0200 Subject: [PATCH 01/14] Add beginner static files --- scripts/web.js | 3 +++ static-parts/beginner-page-end.html | 3 +++ static-parts/beginner-page-start.html | 24 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 static-parts/beginner-page-end.html create mode 100644 static-parts/beginner-page-start.html diff --git a/scripts/web.js b/scripts/web.js index 0081d24ab..58710f9ac 100644 --- a/scripts/web.js +++ b/scripts/web.js @@ -64,6 +64,9 @@ snippets = util.readSnippets(snippetsPath); try { startPart = fs.readFileSync(path.join(staticPartsPath, 'page-start.html'), 'utf8'); endPart = fs.readFileSync(path.join(staticPartsPath, 'page-end.html'), 'utf8'); + + beginnerStaticPart = fs.readFileSync(path.join(staticPartsPath, 'beginner-page-start.html'), 'utf8'); + endStaticPart = fs.readFileSync(path.join(staticPartsPath, 'beginner-page-end.html'), 'utf8'); } catch (err) { // Handle errors (hopefully not!) console.log(`${chalk.red('ERROR!')} During static part loading: ${err}`); diff --git a/static-parts/beginner-page-end.html b/static-parts/beginner-page-end.html new file mode 100644 index 000000000..886e2d2c7 --- /dev/null +++ b/static-parts/beginner-page-end.html @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/static-parts/beginner-page-start.html b/static-parts/beginner-page-start.html new file mode 100644 index 000000000..d367decd7 --- /dev/null +++ b/static-parts/beginner-page-start.html @@ -0,0 +1,24 @@ + + + + + 30 seconds of code - Beginner Section + + + + + + + + + + + +
+
+
+

Beginner snippets

+

The following section is aimed towards individuals who are at the start of their web developer journey.


+
+
+
\ No newline at end of file From 9a557401796593080a3ff7355b0c1a22c968fe17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Sat, 7 Apr 2018 11:27:57 +0200 Subject: [PATCH 02/14] Update static files --- static-parts/beginner-page-start.html | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/static-parts/beginner-page-start.html b/static-parts/beginner-page-start.html index d367decd7..c6bad26ea 100644 --- a/static-parts/beginner-page-start.html +++ b/static-parts/beginner-page-start.html @@ -14,11 +14,16 @@ -
-
-
-

Beginner snippets

-

The following section is aimed towards individuals who are at the start of their web developer journey.


-
+
+

logo 30 seconds of code

+

Curated collection of useful JavaScript snippets
that you can understand in 30 seconds or less.

+
-
\ No newline at end of file +
+
+
+

Beginner snippets

+

The following section is aimed towards individuals who are at the start of their web developer journey.


+
+
+
\ No newline at end of file From 9e583c2720106546bf434c068ec033f5b0f00cf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Sat, 7 Apr 2018 11:57:34 +0200 Subject: [PATCH 03/14] build beginner.html --- docs/beginner.html | 32 ++++++++++++++++++++++++++++++++ scripts/web.js | 24 ++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 docs/beginner.html diff --git a/docs/beginner.html b/docs/beginner.html new file mode 100644 index 000000000..55553fe5b --- /dev/null +++ b/docs/beginner.html @@ -0,0 +1,32 @@ + + + + + 30 seconds of code - Beginner Section + + + + + + + + + + + +
+

logo 30 seconds of code

+

Curated collection of useful JavaScript snippets
that you can understand in 30 seconds or less.

+ +
+
+
+
+

Beginner snippets

+

The following section is aimed towards individuals who are at the start of their web developer journey.


+
+
+
+ + + \ No newline at end of file diff --git a/scripts/web.js b/scripts/web.js index 58710f9ac..53c8e4e61 100644 --- a/scripts/web.js +++ b/scripts/web.js @@ -51,9 +51,13 @@ const snippetsPath = './snippets', docsPath = './docs'; // Set variables for script let snippets = {}, + beginnerSnippets = ['everyNth', 'filterNonUnique', 'last', 'maxN', 'minN', 'nthElement', 'sample', 'similarity', 'tail', 'currentURL', 'hasClass', 'getMeridiemSuffixOfInteger', 'factorial', 'fibonacci', 'gcd', 'isDivisible', 'isEven', 'isPrime', 'lcm', 'randomIntegerInRange', 'sum', 'reverseString', 'truncateString'], startPart = '', endPart = '', output = '', + startBegginerPart = '', + endBegginerPart = '', + beginnerOutput = '', pagesOutput = []; tagDbData = {}; // Start the timer of the script @@ -65,8 +69,8 @@ try { startPart = fs.readFileSync(path.join(staticPartsPath, 'page-start.html'), 'utf8'); endPart = fs.readFileSync(path.join(staticPartsPath, 'page-end.html'), 'utf8'); - beginnerStaticPart = fs.readFileSync(path.join(staticPartsPath, 'beginner-page-start.html'), 'utf8'); - endStaticPart = fs.readFileSync(path.join(staticPartsPath, 'beginner-page-end.html'), 'utf8'); + startBeginnerPart = fs.readFileSync(path.join(staticPartsPath, 'beginner-page-start.html'), 'utf8'); + endBeginnerPart = fs.readFileSync(path.join(staticPartsPath, 'beginner-page-end.html'), 'utf8'); } catch (err) { // Handle errors (hopefully not!) console.log(`${chalk.red('ERROR!')} During static part loading: ${err}`); @@ -160,6 +164,22 @@ try { console.log(`${chalk.red('ERROR!')} During index.html generation: ${err}`); process.exit(1); } + +// Create the output for the beginner.html file + +try { + // Add the static part + beginnerOutput += `${startBeginnerPart + '\n'}`; + beginnerOutput += `${endBeginnerPart}`; + + fs.writeFileSync(path.join(docsPath, 'beginner.html'), beginnerOutput); + + +} catch (err) { + console.log(`${chalk.red('ERROR!')} During beginner.html generation: ${err}`); + process.exit(1); +} + // Log a success message console.log(`${chalk.green('SUCCESS!')} index.html file generated!`); // Log the time taken From 53f8f5676fcdb765e975fd1f5ca59be80b8442c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Sat, 7 Apr 2018 12:04:42 +0200 Subject: [PATCH 04/14] update web.js --- docs/beginner.html | 2 +- scripts/web.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/beginner.html b/docs/beginner.html index 55553fe5b..4a6ded7d2 100644 --- a/docs/beginner.html +++ b/docs/beginner.html @@ -27,6 +27,6 @@
- +
\ No newline at end of file diff --git a/scripts/web.js b/scripts/web.js index 53c8e4e61..b72a14be6 100644 --- a/scripts/web.js +++ b/scripts/web.js @@ -170,8 +170,16 @@ try { try { // Add the static part beginnerOutput += `${startBeginnerPart + '\n'}`; + + beginnerOutput += + '
' + + // begginer snippet goes here. + '' + + '
'; + beginnerOutput += `${endBeginnerPart}`; + // Generate fs.writeFileSync(path.join(docsPath, 'beginner.html'), beginnerOutput); From e73e09fa2f295d744b6eed513d5847f8543245b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Sat, 7 Apr 2018 12:51:41 +0200 Subject: [PATCH 05/14] Update web.js, generate beginner.html --- docs/beginner.html | 187 +++++++++++++++++++++++++- scripts/web.js | 30 ++++- static-parts/beginner-page-start.html | 2 +- 3 files changed, 213 insertions(+), 6 deletions(-) diff --git a/docs/beginner.html b/docs/beginner.html index 4a6ded7d2..3e6c16604 100644 --- a/docs/beginner.html +++ b/docs/beginner.html @@ -17,7 +17,7 @@

logo 30 seconds of code

Curated collection of useful JavaScript snippets
that you can understand in 30 seconds or less.

- +
@@ -27,6 +27,189 @@
-
+

currentURL

+

Returns the current URL.

+

Use window.location.href to get current URL.

+
const currentURL = () => window.location.href;
+
currentURL(); // 'https://google.com'
+
+

everyNth

+

Returns every nth element in an array.

+

Use Array.filter() to create a new array that contains every nth element of a given array.

+
const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
+
everyNth([1, 2, 3, 4, 5, 6], 2); // [ 2, 4, 6 ]
+
+

factorial

+

Calculates the factorial of a number.

+

Use recursion. +If n is less than or equal to 1, return 1. +Otherwise, return the product of n and the factorial of n - 1. +Throws an exception if n is a negative number.

+
const factorial = n =>
+  n < 0
+    ? (() => {
+        throw new TypeError('Negative numbers are not allowed!');
+      })()
+    : n <= 1 ? 1 : n * factorial(n - 1);
+
factorial(6); // 720
+
+

fibonacci

+

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.

+
const fibonacci = n =>
+  Array.from({ length: n }).reduce(
+    (acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
+    []
+  );
+
fibonacci(6); // [0, 1, 1, 2, 3, 5]
+
+

filterNonUnique

+

Filters out the non-unique values in an array.

+

Use Array.filter() for an array containing only the unique values.

+
const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
+
filterNonUnique([1, 2, 2, 3, 4, 4, 5]); // [1,3,5]
+
+

gcd

+

Calculates the greatest common divisor between two or more numbers/arrays.

+

The inner _gcd function uses recursion. +Base case is when y equals 0. In this case, return x. +Otherwise, return the GCD of y and the remainder of the division x/y.

+
const gcd = (...arr) => {
+  const _gcd = (x, y) => (!y ? x : gcd(y, x % y));
+  return [...arr].reduce((a, b) => _gcd(a, b));
+};
+
gcd(8, 36); // 4
+gcd(...[12, 8, 32]); // 4
+
+

getMeridiemSuffixOfInteger

+

Converts an integer to a suffixed string, adding am or pm based on its value.

+

Use the modulo operator (%) and conditional checks to transform an integer to a stringified 12-hour format with meridiem suffix.

+
const getMeridiemSuffixOfInteger = num =>
+  num === 0 || num === 24
+    ? 12 + 'am'
+    : num === 12 ? 12 + 'pm' : num < 12 ? num % 12 + 'am' : num % 12 + 'pm';
+
getMeridiemSuffixOfInteger(0); // "12am"
+getMeridiemSuffixOfInteger(11); // "11am"
+getMeridiemSuffixOfInteger(13); // "1pm"
+getMeridiemSuffixOfInteger(25); // "1pm"
+
+

hasClass

+

Returns true if the element has the specified class, false otherwise.

+

Use element.classList.contains() to check if the element has the specified class.

+
const hasClass = (el, className) => el.classList.contains(className);
+
hasClass(document.querySelector('p.special'), 'special'); // true
+
+

isDivisible

+

Checks if the first numeric argument is divisible by the second one.

+

Use the modulo operator (%) to check if the remainder is equal to 0.

+
const isDivisible = (dividend, divisor) => dividend % divisor === 0;
+
isDivisible(6, 3); // true
+
+

isEven

+

Returns true if the given number is even, false otherwise.

+

Checks whether a number is odd or even using the modulo (%) operator. +Returns true if the number is even, false if the number is odd.

+
const isEven = num => num % 2 === 0;
+
isEven(3); // false
+
+

isPrime

+

Checks if the provided integer is a prime number.

+

Check numbers from 2 to the square root of the given number. +Return false if any of them divides the given number, else return true, unless the number is less than 2.

+
const isPrime = num => {
+  const boundary = Math.floor(Math.sqrt(num));
+  for (var i = 2; i <= boundary; i++) if (num % i === 0) return false;
+  return num >= 2;
+};
+
isPrime(11); // true
+
+

lcm

+

Returns the least common multiple of two or more numbers.

+

Use the greatest common divisor (GCD) formula and the fact that lcm(x,y) = x * y / gcd(x,y) to determine the least common multiple. +The GCD formula uses recursion.

+
const lcm = (...arr) => {
+  const gcd = (x, y) => (!y ? x : gcd(y, x % y));
+  const _lcm = (x, y) => x * y / gcd(x, y);
+  return [...arr].reduce((a, b) => _lcm(a, b));
+};
+
lcm(12, 7); // 84
+lcm(...[1, 3, 4, 5]); // 60
+
+

maxN

+

Returns the n maximum elements from the provided array. If n is greater than or equal to the provided array's length, then return the original array(sorted in descending order).

+

Use Array.sort() combined with the spread operator (...) to create a shallow clone of the array and sort it in descending order. +Use Array.slice() to get the specified number of elements. +Omit the second argument, n, to get a one-element array.

+
const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);
+
maxN([1, 2, 3]); // [3]
+maxN([1, 2, 3], 2); // [3,2]
+
+

minN

+

Returns the n minimum elements from the provided array. If n is greater than or equal to the provided array's length, then return the original array(sorted in ascending order).

+

Use Array.sort() combined with the spread operator (...) to create a shallow clone of the array and sort it in ascending order. +Use Array.slice() to get the specified number of elements. +Omit the second argument, n, to get a one-element array.

+
const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
+
minN([1, 2, 3]); // [1]
+minN([1, 2, 3], 2); // [1,2]
+
+

nthElement

+

Returns the nth element of an array.

+

Use Array.slice() to get an array containing the nth element at the first place. +If the index is out of bounds, return []. +Omit the second argument, n, to get the first element of the array.

+
const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0];
+
nthElement(['a', 'b', 'c'], 1); // 'b'
+nthElement(['a', 'b', 'b'], -3); // 'a'
+
+

randomIntegerInRange

+

Returns a random integer in the specified range.

+

Use Math.random() to generate a random number and map it to the desired range, using Math.floor() to make it an integer.

+
const randomIntegerInRange = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
+
randomIntegerInRange(0, 5); // 2
+
+

reverseString

+

Reverses a string.

+

Use the spread operator (...) and Array.reverse() to reverse the order of the characters in the string. +Combine characters to get a string using String.join('').

+
const reverseString = str => [...str].reverse().join('');
+
reverseString('foobar'); // 'raboof'
+
+

sample

+

Returns a random element from an array.

+

Use Math.random() to generate a random number, multiply it by length and round it of to the nearest whole number using Math.floor(). +This method also works with strings.

+
const sample = arr => arr[Math.floor(Math.random() * arr.length)];
+
sample([3, 7, 9, 11]); // 9
+
+

similarity

+

Returns an array of elements that appear in both arrays.

+

Use Array.filter() to remove values that are not part of values, determined using Array.includes().

+
const similarity = (arr, values) => arr.filter(v => values.includes(v));
+
similarity([1, 2, 3], [1, 2, 4]); // [1,2]
+
+

sum

+

Returns the sum of two or more numbers/arrays.

+

Use Array.reduce() to add each value to an accumulator, initialized with a value of 0.

+
const sum = (...arr) => [...arr].reduce((acc, val) => acc + val, 0);
+
sum(...[1, 2, 3, 4]); // 10
+
+

tail

+

Returns all elements in an array except for the first one.

+

Return Array.slice(1) if the array's length is more than 1, otherwise, return the whole array.

+
const tail = arr => (arr.length > 1 ? arr.slice(1) : arr);
+
tail([1, 2, 3]); // [2,3]
+tail([1]); // [1]
+
+

truncateString

+

Truncates a string up to a specified length.

+

Determine if the string's length is greater than num. +Return the string truncated to the desired length, with '...' appended to the end or the original string.

+
const truncateString = (str, num) =>
+  str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str;
+
truncateString('boomerang', 7); // 'boom...'
+
+
\ No newline at end of file diff --git a/scripts/web.js b/scripts/web.js index b72a14be6..6f74c8578 100644 --- a/scripts/web.js +++ b/scripts/web.js @@ -51,11 +51,11 @@ const snippetsPath = './snippets', docsPath = './docs'; // Set variables for script let snippets = {}, - beginnerSnippets = ['everyNth', 'filterNonUnique', 'last', 'maxN', 'minN', 'nthElement', 'sample', 'similarity', 'tail', 'currentURL', 'hasClass', 'getMeridiemSuffixOfInteger', 'factorial', 'fibonacci', 'gcd', 'isDivisible', 'isEven', 'isPrime', 'lcm', 'randomIntegerInRange', 'sum', 'reverseString', 'truncateString'], + beginnerSnippetNames = ['everyNth', 'filterNonUnique', 'last.md', 'maxN', 'minN', 'nthElement', 'sample', 'similarity', 'tail', 'currentURL', 'hasClass', 'getMeridiemSuffixOfInteger', 'factorial', 'fibonacci', 'gcd', 'isDivisible', 'isEven', 'isPrime', 'lcm', 'randomIntegerInRange', 'sum', 'reverseString', 'truncateString'], startPart = '', endPart = '', output = '', - startBegginerPart = '', + startBeginnerPart = '', endBegginerPart = '', beginnerOutput = '', pagesOutput = []; @@ -64,6 +64,7 @@ let snippets = {}, console.time('Webber'); // Synchronously read all snippets and sort them as necessary (case-insensitive) snippets = util.readSnippets(snippetsPath); + // Load static parts for the index.html file try { startPart = fs.readFileSync(path.join(staticPartsPath, 'page-start.html'), 'utf8'); @@ -171,6 +172,29 @@ try { // Add the static part beginnerOutput += `${startBeginnerPart + '\n'}`; + // Filter begginer snippets + const filteredBeginnerSnippets = Object.keys(snippets) + .filter(key => beginnerSnippetNames.map(name => name+'.md').includes(key)) + .reduce((obj, key) => { + obj[key] = snippets[key]; + return obj; + }, {}); + + console.log(filteredBeginnerSnippets); + for (let snippet of Object.entries(filteredBeginnerSnippets)) + beginnerOutput += + '
' + + md + .render(`\n${snippets[snippet[0]]}`) + .replace(/

/g, `${snippet[1].includes('advanced')?'advanced':''}

`) + .replace(/<\/h3>/g, '
') + .replace(/
([^\0]*?)<\/code><\/pre>/gm, (match, p1) => `
${Prism.highlight(unescapeHTML(p1), Prism.languages.javascript)}
`) + .replace(/<\/pre>\s+
📋 Copy to clipboard' +
+          '
'; + + beginnerOutput += '
' + // begginer snippet goes here. @@ -179,7 +203,7 @@ try { beginnerOutput += `${endBeginnerPart}`; - // Generate + // Generate 'beginner.html' file fs.writeFileSync(path.join(docsPath, 'beginner.html'), beginnerOutput); diff --git a/static-parts/beginner-page-start.html b/static-parts/beginner-page-start.html index c6bad26ea..aefd3736f 100644 --- a/static-parts/beginner-page-start.html +++ b/static-parts/beginner-page-start.html @@ -17,7 +17,7 @@

logo 30 seconds of code

Curated collection of useful JavaScript snippets
that you can understand in 30 seconds or less.

- +
From adc4b53ceee07617968763d4414f49f7564bc005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Sat, 7 Apr 2018 13:08:33 +0200 Subject: [PATCH 06/14] update static parts and final beginner.html --- docs/beginner.html | 50 ++++++++++++++------------- scripts/web.js | 4 ++- static-parts/beginner-page-end.html | 2 ++ static-parts/beginner-page-start.html | 1 - 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/docs/beginner.html b/docs/beginner.html index 3e6c16604..01274c709 100644 --- a/docs/beginner.html +++ b/docs/beginner.html @@ -26,20 +26,20 @@

The following section is aimed towards individuals who are at the start of their web developer journey.


-
-

currentURL

+ +

currentURL

Returns the current URL.

Use window.location.href to get current URL.

const currentURL = () => window.location.href;
 
currentURL(); // 'https://google.com'
 
-

everyNth

+

everyNth

Returns every nth element in an array.

Use Array.filter() to create a new array that contains every nth element of a given array.

const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
 
everyNth([1, 2, 3, 4, 5, 6], 2); // [ 2, 4, 6 ]
 
-

factorial

+

factorial

Calculates the factorial of a number.

Use recursion. If n is less than or equal to 1, return 1. @@ -53,7 +53,7 @@ Throws an exception if n is a negative number.

: n <= 1 ? 1 : n * factorial(n - 1);
factorial(6); // 720
 
-

fibonacci

+

fibonacci

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.

@@ -64,13 +64,13 @@ Use Array.reduce() to add values into the array, using the sum of t );
fibonacci(6); // [0, 1, 1, 2, 3, 5]
 
-

filterNonUnique

+

filterNonUnique

Filters out the non-unique values in an array.

Use Array.filter() for an array containing only the unique values.

const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
 
filterNonUnique([1, 2, 2, 3, 4, 4, 5]); // [1,3,5]
 
-

gcd

+

gcd

Calculates the greatest common divisor between two or more numbers/arrays.

The inner _gcd function uses recursion. Base case is when y equals 0. In this case, return x. @@ -82,7 +82,7 @@ Otherwise, return the GCD of y and the remainder of the division

gcd(8, 36); // 4
 gcd(...[12, 8, 32]); // 4
 
-

getMeridiemSuffixOfInteger

+

getMeridiemSuffixOfInteger

Converts an integer to a suffixed string, adding am or pm based on its value.

Use the modulo operator (%) and conditional checks to transform an integer to a stringified 12-hour format with meridiem suffix.

const getMeridiemSuffixOfInteger = num =>
@@ -94,26 +94,26 @@ Otherwise, return the GCD of y and the remainder of the division getMeridiemSuffixOfInteger(13); // "1pm"
 getMeridiemSuffixOfInteger(25); // "1pm"
 
-

hasClass

+

hasClass

Returns true if the element has the specified class, false otherwise.

Use element.classList.contains() to check if the element has the specified class.

const hasClass = (el, className) => el.classList.contains(className);
 
hasClass(document.querySelector('p.special'), 'special'); // true
 
-

isDivisible

+

isDivisible

Checks if the first numeric argument is divisible by the second one.

Use the modulo operator (%) to check if the remainder is equal to 0.

const isDivisible = (dividend, divisor) => dividend % divisor === 0;
 
isDivisible(6, 3); // true
 
-

isEven

+

isEven

Returns true if the given number is even, false otherwise.

Checks whether a number is odd or even using the modulo (%) operator. Returns true if the number is even, false if the number is odd.

const isEven = num => num % 2 === 0;
 
isEven(3); // false
 
-

isPrime

+

isPrime

Checks if the provided integer is a prime number.

Check numbers from 2 to the square root of the given number. Return false if any of them divides the given number, else return true, unless the number is less than 2.

@@ -124,7 +124,7 @@ Return false if any of them divides the given number, else return < };
isPrime(11); // true
 
-

lcm

+

lcm

Returns the least common multiple of two or more numbers.

Use the greatest common divisor (GCD) formula and the fact that lcm(x,y) = x * y / gcd(x,y) to determine the least common multiple. The GCD formula uses recursion.

@@ -136,7 +136,7 @@ The GCD formula uses recursion.

lcm(12, 7); // 84
 lcm(...[1, 3, 4, 5]); // 60
 
-

maxN

+

maxN

Returns the n maximum elements from the provided array. If n is greater than or equal to the provided array's length, then return the original array(sorted in descending order).

Use Array.sort() combined with the spread operator (...) to create a shallow clone of the array and sort it in descending order. Use Array.slice() to get the specified number of elements. @@ -145,7 +145,7 @@ Omit the second argument, n, to get a one-element array.

maxN([1, 2, 3]); // [3]
 maxN([1, 2, 3], 2); // [3,2]
 
-

minN

+

minN

Returns the n minimum elements from the provided array. If n is greater than or equal to the provided array's length, then return the original array(sorted in ascending order).

Use Array.sort() combined with the spread operator (...) to create a shallow clone of the array and sort it in ascending order. Use Array.slice() to get the specified number of elements. @@ -154,7 +154,7 @@ Omit the second argument, n, to get a one-element array.

minN([1, 2, 3]); // [1]
 minN([1, 2, 3], 2); // [1,2]
 
-

nthElement

+

nthElement

Returns the nth element of an array.

Use Array.slice() to get an array containing the nth element at the first place. If the index is out of bounds, return []. @@ -163,46 +163,46 @@ Omit the second argument, n, to get the first element of the array.

nthElement(['a', 'b', 'c'], 1); // 'b'
 nthElement(['a', 'b', 'b'], -3); // 'a'
 
-

randomIntegerInRange

+

randomIntegerInRange

Returns a random integer in the specified range.

Use Math.random() to generate a random number and map it to the desired range, using Math.floor() to make it an integer.

const randomIntegerInRange = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
 
randomIntegerInRange(0, 5); // 2
 
-

reverseString

+

reverseString

Reverses a string.

Use the spread operator (...) and Array.reverse() to reverse the order of the characters in the string. Combine characters to get a string using String.join('').

const reverseString = str => [...str].reverse().join('');
 
reverseString('foobar'); // 'raboof'
 
-

sample

+

sample

Returns a random element from an array.

Use Math.random() to generate a random number, multiply it by length and round it of to the nearest whole number using Math.floor(). This method also works with strings.

const sample = arr => arr[Math.floor(Math.random() * arr.length)];
 
sample([3, 7, 9, 11]); // 9
 
-

similarity

+

similarity

Returns an array of elements that appear in both arrays.

Use Array.filter() to remove values that are not part of values, determined using Array.includes().

const similarity = (arr, values) => arr.filter(v => values.includes(v));
 
similarity([1, 2, 3], [1, 2, 4]); // [1,2]
 
-

sum

+

sum

Returns the sum of two or more numbers/arrays.

Use Array.reduce() to add each value to an accumulator, initialized with a value of 0.

const sum = (...arr) => [...arr].reduce((acc, val) => acc + val, 0);
 
sum(...[1, 2, 3, 4]); // 10
 
-

tail

+

tail

Returns all elements in an array except for the first one.

Return Array.slice(1) if the array's length is more than 1, otherwise, return the whole array.

const tail = arr => (arr.length > 1 ? arr.slice(1) : arr);
 
tail([1, 2, 3]); // [2,3]
 tail([1]); // [1]
 
-

truncateString

+

truncateString

Truncates a string up to a specified length.

Determine if the string's length is greater than num. Return the string truncated to the desired length, with '...' appended to the end or the original string.

@@ -210,6 +210,8 @@ Return the string truncated to the desired length, with '...' appen str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str;
truncateString('boomerang', 7); // 'boom...'
 
-
+
+ + \ No newline at end of file diff --git a/scripts/web.js b/scripts/web.js index 6f74c8578..5822589a8 100644 --- a/scripts/web.js +++ b/scripts/web.js @@ -183,6 +183,8 @@ try { console.log(filteredBeginnerSnippets); for (let snippet of Object.entries(filteredBeginnerSnippets)) beginnerOutput += + '
' + + '
' + '
' + md .render(`\n${snippets[snippet[0]]}`) @@ -192,7 +194,7 @@ try { .replace(/
([^\0]*?)<\/code><\/pre>/gm, (match, p1) => `
${Prism.highlight(unescapeHTML(p1), Prism.languages.javascript)}
`) .replace(/<\/pre>\s+
📋 Copy to clipboard' +
-          '
'; + '
'; beginnerOutput += diff --git a/static-parts/beginner-page-end.html b/static-parts/beginner-page-end.html index 886e2d2c7..8c5b3ff89 100644 --- a/static-parts/beginner-page-end.html +++ b/static-parts/beginner-page-end.html @@ -1,3 +1,5 @@ + + \ No newline at end of file diff --git a/static-parts/beginner-page-start.html b/static-parts/beginner-page-start.html index aefd3736f..7a49e98e8 100644 --- a/static-parts/beginner-page-start.html +++ b/static-parts/beginner-page-start.html @@ -26,4 +26,3 @@

The following section is aimed towards individuals who are at the start of their web developer journey.


- \ No newline at end of file From b8d0d87136c36e535c5fa8e4ce5c258b249e417f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Sat, 7 Apr 2018 13:27:56 +0200 Subject: [PATCH 07/14] update beginner page --- docs/beginner.html | 43 ++++++++++++++++++++++++++- scripts/web.js | 19 ++++-------- static-parts/beginner-page-start.html | 41 +++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 14 deletions(-) diff --git a/docs/beginner.html b/docs/beginner.html index 01274c709..16c06e47d 100644 --- a/docs/beginner.html +++ b/docs/beginner.html @@ -12,6 +12,47 @@ +
@@ -210,7 +251,7 @@ Return the string truncated to the desired length, with '...' appen str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str;
truncateString('boomerang', 7); // 'boom...'
 
-
+ diff --git a/scripts/web.js b/scripts/web.js index 5822589a8..a2d654366 100644 --- a/scripts/web.js +++ b/scripts/web.js @@ -55,8 +55,8 @@ let snippets = {}, startPart = '', endPart = '', output = '', - startBeginnerPart = '', - endBegginerPart = '', + beginnerStartPart = '', + beginnerEndPart = '', beginnerOutput = '', pagesOutput = []; tagDbData = {}; @@ -70,8 +70,8 @@ try { startPart = fs.readFileSync(path.join(staticPartsPath, 'page-start.html'), 'utf8'); endPart = fs.readFileSync(path.join(staticPartsPath, 'page-end.html'), 'utf8'); - startBeginnerPart = fs.readFileSync(path.join(staticPartsPath, 'beginner-page-start.html'), 'utf8'); - endBeginnerPart = fs.readFileSync(path.join(staticPartsPath, 'beginner-page-end.html'), 'utf8'); + beginnerStartPart = fs.readFileSync(path.join(staticPartsPath, 'beginner-page-start.html'), 'utf8'); + beginnerEndPart = fs.readFileSync(path.join(staticPartsPath, 'beginner-page-end.html'), 'utf8'); } catch (err) { // Handle errors (hopefully not!) console.log(`${chalk.red('ERROR!')} During static part loading: ${err}`); @@ -170,7 +170,7 @@ try { try { // Add the static part - beginnerOutput += `${startBeginnerPart + '\n'}`; + beginnerOutput += `${beginnerStartPart + '\n'}`; // Filter begginer snippets const filteredBeginnerSnippets = Object.keys(snippets) @@ -180,7 +180,6 @@ try { return obj; }, {}); - console.log(filteredBeginnerSnippets); for (let snippet of Object.entries(filteredBeginnerSnippets)) beginnerOutput += '
' + @@ -197,13 +196,7 @@ try { '
'; - beginnerOutput += - '
' + - // begginer snippet goes here. - '' + - '
'; - - beginnerOutput += `${endBeginnerPart}`; + beginnerOutput += `${beginnerEndPart}`; // Generate 'beginner.html' file fs.writeFileSync(path.join(docsPath, 'beginner.html'), beginnerOutput); diff --git a/static-parts/beginner-page-start.html b/static-parts/beginner-page-start.html index 7a49e98e8..edd03d57e 100644 --- a/static-parts/beginner-page-start.html +++ b/static-parts/beginner-page-start.html @@ -12,6 +12,47 @@ +
From 11e182457cc6f2b2444c8d2569267842162fe1b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Sat, 7 Apr 2018 14:27:39 +0200 Subject: [PATCH 08/14] =?UTF-8?q?=F0=9F=90=9B=20=20toggle=20example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/beginner.html | 168 +++++++++++--------------- scripts/web.js | 6 + static-parts/beginner-page-end.html | 1 + static-parts/beginner-page-start.html | 43 +------ 4 files changed, 82 insertions(+), 136 deletions(-) diff --git a/docs/beginner.html b/docs/beginner.html index 16c06e47d..5aa9a67aa 100644 --- a/docs/beginner.html +++ b/docs/beginner.html @@ -13,48 +13,17 @@ + }, false); + } + - + +

logo 30 seconds of code

Curated collection of useful JavaScript snippets
that you can understand in 30 seconds or less.

@@ -71,14 +40,14 @@

currentURL

Returns the current URL.

Use window.location.href to get current URL.

-
const currentURL = () => window.location.href;
-
currentURL(); // 'https://google.com'
+
const currentURL = () => window.location.href;
+
currentURL(); // 'https://google.com'
 

everyNth

Returns every nth element in an array.

Use Array.filter() to create a new array that contains every nth element of a given array.

-
const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
-
everyNth([1, 2, 3, 4, 5, 6], 2); // [ 2, 4, 6 ]
+
const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
+
everyNth([1, 2, 3, 4, 5, 6], 2); // [ 2, 4, 6 ]
 

factorial

Calculates the factorial of a number.

@@ -88,28 +57,28 @@ Otherwise, return the product of n and the factorial of n - 1 Throws an exception if n is a negative number.

const factorial = n =>
   n < 0
-    ? (() => {
-        throw new TypeError('Negative numbers are not allowed!');
-      })()
-    : n <= 1 ? 1 : n * factorial(n - 1);
-
factorial(6); // 720
+    ? (() => {
+        throw new TypeError('Negative numbers are not allowed!');
+      })()
+    : n <= 1 ? 1 : n * factorial(n - 1);
+
factorial(6); // 720
 

fibonacci

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.

const fibonacci = n =>
-  Array.from({ length: n }).reduce(
-    (acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
-    []
-  );
-
fibonacci(6); // [0, 1, 1, 2, 3, 5]
+  Array.from({ length: n }).reduce(
+    (acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
+    []
+  );
+
fibonacci(6); // [0, 1, 1, 2, 3, 5]
 

filterNonUnique

Filters out the non-unique values in an array.

Use Array.filter() for an array containing only the unique values.

-
const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
-
filterNonUnique([1, 2, 2, 3, 4, 4, 5]); // [1,3,5]
+
const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
+
filterNonUnique([1, 2, 2, 3, 4, 4, 5]); // [1,3,5]
 

gcd

Calculates the greatest common divisor between two or more numbers/arrays.

@@ -117,11 +86,11 @@ Use Array.reduce() to add values into the array, using the sum of t Base case is when y equals 0. In this case, return x. Otherwise, return the GCD of y and the remainder of the division x/y.

const gcd = (...arr) => {
-  const _gcd = (x, y) => (!y ? x : gcd(y, x % y));
-  return [...arr].reduce((a, b) => _gcd(a, b));
-};
-
gcd(8, 36); // 4
-gcd(...[12, 8, 32]); // 4
+  const _gcd = (x, y) => (!y ? x : gcd(y, x % y));
+  return [...arr].reduce((a, b) => _gcd(a, b));
+};
+
gcd(8, 36); // 4
+gcd(...[12, 8, 32]); // 4
 

getMeridiemSuffixOfInteger

Converts an integer to a suffixed string, adding am or pm based on its value.

@@ -130,118 +99,118 @@ Otherwise, return the GCD of y and the remainder of the division === 0 || num === 24 ? 12 + 'am' : num === 12 ? 12 + 'pm' : num < 12 ? num % 12 + 'am' : num % 12 + 'pm'; -
getMeridiemSuffixOfInteger(0); // "12am"
-getMeridiemSuffixOfInteger(11); // "11am"
-getMeridiemSuffixOfInteger(13); // "1pm"
-getMeridiemSuffixOfInteger(25); // "1pm"
+
getMeridiemSuffixOfInteger(0); // "12am"
+getMeridiemSuffixOfInteger(11); // "11am"
+getMeridiemSuffixOfInteger(13); // "1pm"
+getMeridiemSuffixOfInteger(25); // "1pm"
 

hasClass

Returns true if the element has the specified class, false otherwise.

Use element.classList.contains() to check if the element has the specified class.

-
const hasClass = (el, className) => el.classList.contains(className);
-
hasClass(document.querySelector('p.special'), 'special'); // true
+
const hasClass = (el, className) => el.classList.contains(className);
+
hasClass(document.querySelector('p.special'), 'special'); // true
 

isDivisible

Checks if the first numeric argument is divisible by the second one.

Use the modulo operator (%) to check if the remainder is equal to 0.

const isDivisible = (dividend, divisor) => dividend % divisor === 0;
-
isDivisible(6, 3); // true
+
isDivisible(6, 3); // true
 

isEven

Returns true if the given number is even, false otherwise.

Checks whether a number is odd or even using the modulo (%) operator. Returns true if the number is even, false if the number is odd.

const isEven = num => num % 2 === 0;
-
isEven(3); // false
+
isEven(3); // false
 

isPrime

Checks if the provided integer is a prime number.

Check numbers from 2 to the square root of the given number. Return false if any of them divides the given number, else return true, unless the number is less than 2.

const isPrime = num => {
-  const boundary = Math.floor(Math.sqrt(num));
+  const boundary = Math.floor(Math.sqrt(num));
   for (var i = 2; i <= boundary; i++) if (num % i === 0) return false;
-  return num >= 2;
-};
-
isPrime(11); // true
+  return num >= 2;
+};
+
isPrime(11); // true
 

lcm

Returns the least common multiple of two or more numbers.

Use the greatest common divisor (GCD) formula and the fact that lcm(x,y) = x * y / gcd(x,y) to determine the least common multiple. The GCD formula uses recursion.

const lcm = (...arr) => {
-  const gcd = (x, y) => (!y ? x : gcd(y, x % y));
-  const _lcm = (x, y) => x * y / gcd(x, y);
-  return [...arr].reduce((a, b) => _lcm(a, b));
-};
-
lcm(12, 7); // 84
-lcm(...[1, 3, 4, 5]); // 60
+  const gcd = (x, y) => (!y ? x : gcd(y, x % y));
+  const _lcm = (x, y) => x * y / gcd(x, y);
+  return [...arr].reduce((a, b) => _lcm(a, b));
+};
+
lcm(12, 7); // 84
+lcm(...[1, 3, 4, 5]); // 60
 

maxN

Returns the n maximum elements from the provided array. If n is greater than or equal to the provided array's length, then return the original array(sorted in descending order).

Use Array.sort() combined with the spread operator (...) to create a shallow clone of the array and sort it in descending order. Use Array.slice() to get the specified number of elements. Omit the second argument, n, to get a one-element array.

-
const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);
-
maxN([1, 2, 3]); // [3]
-maxN([1, 2, 3], 2); // [3,2]
+
const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);
+
maxN([1, 2, 3]); // [3]
+maxN([1, 2, 3], 2); // [3,2]
 

minN

Returns the n minimum elements from the provided array. If n is greater than or equal to the provided array's length, then return the original array(sorted in ascending order).

Use Array.sort() combined with the spread operator (...) to create a shallow clone of the array and sort it in ascending order. Use Array.slice() to get the specified number of elements. Omit the second argument, n, to get a one-element array.

-
const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
-
minN([1, 2, 3]); // [1]
-minN([1, 2, 3], 2); // [1,2]
+
const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
+
minN([1, 2, 3]); // [1]
+minN([1, 2, 3], 2); // [1,2]
 

nthElement

Returns the nth element of an array.

Use Array.slice() to get an array containing the nth element at the first place. If the index is out of bounds, return []. Omit the second argument, n, to get the first element of the array.

-
const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0];
-
nthElement(['a', 'b', 'c'], 1); // 'b'
-nthElement(['a', 'b', 'b'], -3); // 'a'
+
const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0];
+
nthElement(['a', 'b', 'c'], 1); // 'b'
+nthElement(['a', 'b', 'b'], -3); // 'a'
 

randomIntegerInRange

Returns a random integer in the specified range.

Use Math.random() to generate a random number and map it to the desired range, using Math.floor() to make it an integer.

-
const randomIntegerInRange = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
-
randomIntegerInRange(0, 5); // 2
+
const randomIntegerInRange = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
+
randomIntegerInRange(0, 5); // 2
 

reverseString

Reverses a string.

Use the spread operator (...) and Array.reverse() to reverse the order of the characters in the string. Combine characters to get a string using String.join('').

-
const reverseString = str => [...str].reverse().join('');
-
reverseString('foobar'); // 'raboof'
+
const reverseString = str => [...str].reverse().join('');
+
reverseString('foobar'); // 'raboof'
 

sample

Returns a random element from an array.

Use Math.random() to generate a random number, multiply it by length and round it of to the nearest whole number using Math.floor(). This method also works with strings.

-
const sample = arr => arr[Math.floor(Math.random() * arr.length)];
-
sample([3, 7, 9, 11]); // 9
+
const sample = arr => arr[Math.floor(Math.random() * arr.length)];
+
sample([3, 7, 9, 11]); // 9
 

similarity

Returns an array of elements that appear in both arrays.

Use Array.filter() to remove values that are not part of values, determined using Array.includes().

-
const similarity = (arr, values) => arr.filter(v => values.includes(v));
-
similarity([1, 2, 3], [1, 2, 4]); // [1,2]
+
const similarity = (arr, values) => arr.filter(v => values.includes(v));
+
similarity([1, 2, 3], [1, 2, 4]); // [1,2]
 

sum

Returns the sum of two or more numbers/arrays.

Use Array.reduce() to add each value to an accumulator, initialized with a value of 0.

-
const sum = (...arr) => [...arr].reduce((acc, val) => acc + val, 0);
-
sum(...[1, 2, 3, 4]); // 10
+
const sum = (...arr) => [...arr].reduce((acc, val) => acc + val, 0);
+
sum(...[1, 2, 3, 4]); // 10
 

tail

Returns all elements in an array except for the first one.

Return Array.slice(1) if the array's length is more than 1, otherwise, return the whole array.

-
const tail = arr => (arr.length > 1 ? arr.slice(1) : arr);
-
tail([1, 2, 3]); // [2,3]
-tail([1]); // [1]
+
const tail = arr => (arr.length > 1 ? arr.slice(1) : arr);
+
tail([1, 2, 3]); // [2,3]
+tail([1]); // [1]
 

truncateString

Truncates a string up to a specified length.

@@ -249,10 +218,11 @@ This method also works with strings.

Return the string truncated to the desired length, with '...' appended to the end or the original string.

const truncateString = (str, num) =>
   str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str;
-
truncateString('boomerang', 7); // 'boom...'
+
truncateString('boomerang', 7); // 'boom...'
 
+ \ No newline at end of file diff --git a/scripts/web.js b/scripts/web.js index a2d654366..83c8f824a 100644 --- a/scripts/web.js +++ b/scripts/web.js @@ -195,6 +195,12 @@ try { '' + ''; + beginnerOutput = util.optimizeNodes(beginnerOutput, /([^\0<]*?)<\/span>([\n\r\s]*)([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `${p1}${p2}${p3}`); + // Optimize operator nodes + beginnerOutput = util.optimizeNodes(beginnerOutput, /([^\0<]*?)<\/span>([\n\r\s]*)([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `${p1}${p2}${p3}`); + // Optimize keyword nodes + beginnerOutput = util.optimizeNodes(beginnerOutput, /([^\0<]*?)<\/span>([\n\r\s]*)([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `${p1}${p2}${p3}`); + beginnerOutput += `${beginnerEndPart}`; diff --git a/static-parts/beginner-page-end.html b/static-parts/beginner-page-end.html index 8c5b3ff89..6c016eca2 100644 --- a/static-parts/beginner-page-end.html +++ b/static-parts/beginner-page-end.html @@ -1,5 +1,6 @@ + \ No newline at end of file diff --git a/static-parts/beginner-page-start.html b/static-parts/beginner-page-start.html index edd03d57e..3aed0c0b2 100644 --- a/static-parts/beginner-page-start.html +++ b/static-parts/beginner-page-start.html @@ -13,48 +13,17 @@ + }, false); + } + - + +

logo 30 seconds of code

Curated collection of useful JavaScript snippets
that you can understand in 30 seconds or less.

From 8a66ec0dc309d2d5e24bdf904d2b151d0fc6bf74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Sat, 7 Apr 2018 14:29:58 +0200 Subject: [PATCH 09/14] =?UTF-8?q?=F0=9F=8E=A8=20=20GitHub=20logo=20color?= =?UTF-8?q?=20change?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static-parts/beginner-page-start.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static-parts/beginner-page-start.html b/static-parts/beginner-page-start.html index 3aed0c0b2..f343224e0 100644 --- a/static-parts/beginner-page-start.html +++ b/static-parts/beginner-page-start.html @@ -23,7 +23,7 @@ - +

logo 30 seconds of code

Curated collection of useful JavaScript snippets
that you can understand in 30 seconds or less.

From ec115f0021057ad9307a42660fcab0b4c810ed9d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 7 Apr 2018 15:57:40 +0300 Subject: [PATCH 10/14] Fixed one snippet name not working last.md should now be part of the beginner page --- docs/beginner.html | 8 +++++++- scripts/web.js | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/beginner.html b/docs/beginner.html index 5aa9a67aa..60d04d2b9 100644 --- a/docs/beginner.html +++ b/docs/beginner.html @@ -23,7 +23,7 @@ - +

logo 30 seconds of code

Curated collection of useful JavaScript snippets
that you can understand in 30 seconds or less.

@@ -134,6 +134,12 @@ Return false if any of them divides the given number, else return < };
isPrime(11); // true
 
+

last

+

Returns the last element in an array.

+

Use arr.length - 1 to compute the index of the last element of the given array and returning it.

+
const last = arr => arr[arr.length - 1];
+
last([1, 2, 3]); // 3
+

lcm

Returns the least common multiple of two or more numbers.

Use the greatest common divisor (GCD) formula and the fact that lcm(x,y) = x * y / gcd(x,y) to determine the least common multiple. diff --git a/scripts/web.js b/scripts/web.js index 83c8f824a..b1daa7c50 100644 --- a/scripts/web.js +++ b/scripts/web.js @@ -51,7 +51,7 @@ const snippetsPath = './snippets', docsPath = './docs'; // Set variables for script let snippets = {}, - beginnerSnippetNames = ['everyNth', 'filterNonUnique', 'last.md', 'maxN', 'minN', 'nthElement', 'sample', 'similarity', 'tail', 'currentURL', 'hasClass', 'getMeridiemSuffixOfInteger', 'factorial', 'fibonacci', 'gcd', 'isDivisible', 'isEven', 'isPrime', 'lcm', 'randomIntegerInRange', 'sum', 'reverseString', 'truncateString'], + beginnerSnippetNames = ['everyNth', 'filterNonUnique', 'last', 'maxN', 'minN', 'nthElement', 'sample', 'similarity', 'tail', 'currentURL', 'hasClass', 'getMeridiemSuffixOfInteger', 'factorial', 'fibonacci', 'gcd', 'isDivisible', 'isEven', 'isPrime', 'lcm', 'randomIntegerInRange', 'sum', 'reverseString', 'truncateString'], startPart = '', endPart = '', output = '', From a9824b4191964572e6a00a48f120e90c0a9fc33b Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 7 Apr 2018 16:23:52 +0300 Subject: [PATCH 11/14] Fixed some of the beginner page scripts --- docs/beginner.html | 56 ++++++++++++++++++++++++++- static-parts/beginner-page-start.html | 56 ++++++++++++++++++++++++++- 2 files changed, 108 insertions(+), 4 deletions(-) diff --git a/docs/beginner.html b/docs/beginner.html index 60d04d2b9..1384f7d2e 100644 --- a/docs/beginner.html +++ b/docs/beginner.html @@ -13,16 +13,68 @@ + - +

logo 30 seconds of code

diff --git a/static-parts/beginner-page-start.html b/static-parts/beginner-page-start.html index f343224e0..f2a219cd5 100644 --- a/static-parts/beginner-page-start.html +++ b/static-parts/beginner-page-start.html @@ -13,16 +13,68 @@ + - +

logo 30 seconds of code

From 9bd447b8dbbde4a0acae7b2871806db9b0c81225 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 7 Apr 2018 16:30:56 +0300 Subject: [PATCH 12/14] Fixed scrolling to top for beginner page --- docs/beginner.html | 4 ++-- static-parts/beginner-page-start.html | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/beginner.html b/docs/beginner.html index 1384f7d2e..7f7d25fb8 100644 --- a/docs/beginner.html +++ b/docs/beginner.html @@ -14,10 +14,10 @@