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'}
+
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]);
+}
+
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]
split('') into individual characters, reverse(),
arr.reduce((a, v) => a.concat(a.map(r => [v].concat(r))), [[]]);
// powerset([1,2]) -> [[], [1], [2], [2,1]]
+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]
+
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.