From 1045410b0bc952309c2105ae819a99ae344aa489 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 21 Dec 2017 14:48:27 +0200 Subject: [PATCH] Tag --- README.md | 35 +++++++++++++++++++++++++++++++++++ docs/index.html | 23 ++++++++++++++++++++++- tag_database | 1 + 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ba24c7d61..895d8fc38 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ * [`elementIsVisibleInViewport`](#elementisvisibleinviewport) * [`getScrollPosition`](#getscrollposition) * [`getURLParameters`](#geturlparameters) +* [`httpsRedirect`](#httpsredirect) * [`redirect`](#redirect) * [`scrollToTop`](#scrolltotop) @@ -100,6 +101,7 @@ * [`palindrome`](#palindrome) * [`percentile`](#percentile) * [`powerset`](#powerset) +* [`primes`](#primes) * [`randomIntegerInRange`](#randomintegerinrange) * [`randomNumberInRange`](#randomnumberinrange) * [`round`](#round) @@ -901,6 +903,20 @@ const getURLParameters = url => [⬆ back to top](#table-of-contents) +### httpsRedirect + +Redirects the page to HTTPS if its currently in HTTP. Also, pressing the back button doesn't take it back to the HTTP page as its replaced in the history. + +Use `location.protocol` to get the protocol currently being used. If it's not HTTPS, use `location.replace()` to replace the existing page with the HTTPS version of the page. Use `location.href` to get the full address, split it with `String.split()` and remove the protocol part of the URL. + +```js +const httpsRedirect = () => { + if(location.protocol !== "https:") location.replace("https://" + location.href.split("//")[1]); +} +``` + +[⬆ back to top](#table-of-contents) + ### redirect Redirects to a specified URL. @@ -1439,6 +1455,25 @@ const powerset = arr => [⬆ back to top](#table-of-contents) +### primes + +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. + +```js +const primes = num => { + 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; +} +// primes(10) -> [2,3,5,7] +``` + +[⬆ back to top](#table-of-contents) + ### randomIntegerInRange Returns a random integer in the specified range. diff --git a/docs/index.html b/docs/index.html index 2a083f30b..bad72481f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -92,6 +92,7 @@ elementIsVisibleInViewport getScrollPosition getURLParameters +httpsRedirect redirect scrollToTop @@ -131,6 +132,7 @@ palindrome percentile powerset +primes randomIntegerInRange randomNumberInRange round @@ -593,6 +595,13 @@ Pass location.search as the argument to apply to the current ); // getURLParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'} +

httpsRedirect

+

Redirects the page to HTTPS if its currently in HTTP. Also, pressing the back button doesn't take it back to the HTTP page as its replaced in the history.

+

Use location.protocol to get the protocol currently being used. If it's not HTTPS, use location.replace() to replace the existing page with the HTTPS version of the page. Use location.href to get the full address, split it with String.split() and remove the protocol part of the URL.

+
const httpsRedirect = () => {
+  if(location.protocol !== "https:") location.replace("https://" + location.href.split("//")[1]);
+}
+

redirect

Redirects to a specified URL.

Use window.location.href or window.location.replace() to redirect to url. @@ -789,7 +798,7 @@ Throws an exception if n is a negative number.

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(n).fill(0).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []);
+  Array.from({ length: n}).map(v => 0).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []);
 // fibonacci(5) -> [0,1,1,2,3]
 

gcd

@@ -899,6 +908,18 @@ Then, split('') into individual characters, reverse(), arr.reduce((a, v) => a.concat(a.map(r => [v].concat(r))), [[]]); // powerset([1,2]) -> [[], [1], [2], [2,1]] +

primes

+

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), 
+    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; 
+}
+// primes(10) -> [2,3,5,7] 
+

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.

diff --git a/tag_database b/tag_database index fa488eabf..4c19589ef 100644 --- a/tag_database +++ b/tag_database @@ -81,6 +81,7 @@ percentile:math pick:array pipe:function powerset:math +primes:math promisify:function pull:array pullAtIndex:array