From 911610949818bbd9b27d492bc551baff3506e89d Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 23:00:21 +1100 Subject: [PATCH 01/41] Create standard-deviation.md http://www.calculator.net/standard-deviation-calculator.html As a one-liner it's really long, feel free to optimize the formatting here (or shorten it further somehow). --- snippets/standard-deviation.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 snippets/standard-deviation.md diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md new file mode 100644 index 000000000..3ee95a147 --- /dev/null +++ b/snippets/standard-deviation.md @@ -0,0 +1,16 @@ +### Standard deviation + +Use `Array.reduce()` to calculate the mean of the values, the variance of the values, and the sum of the variance +of the values to determine the standard deviation of an array of numbers. + +NOTE: This is **population standard deviation**. Use `/ (arr.length - 1)` at the end to +calculate **sample standard deviation**. + +```js +const standardDeviation = (arr, val) => + Math.sqrt( + arr.reduce((acc, val) => acc.concat(Math.pow(val - arr.reduce((acc, val) => acc + val, 0) / arr.length, 2)), []) + .reduce((acc, val) => acc + val, 0) + / arr.length + ); +``` From 635adbc61799bfb7659448f38099adb496a80b6a Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 23:02:21 +1100 Subject: [PATCH 02/41] Update standard-deviation.md --- snippets/standard-deviation.md | 1 + 1 file changed, 1 insertion(+) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index 3ee95a147..63485f91e 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -13,4 +13,5 @@ const standardDeviation = (arr, val) => .reduce((acc, val) => acc + val, 0) / arr.length ); +// standardDeviation([10,2,38,23,38,23,21]) -> 12.298996142875 ``` From 731df313959b3b47e4c9ed4da46c9fa941947608 Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 23:12:42 +1100 Subject: [PATCH 03/41] Update standard-deviation.md --- snippets/standard-deviation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index 63485f91e..0ae7f56af 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -7,7 +7,7 @@ NOTE: This is **population standard deviation**. Use `/ (arr.length - 1)` at the calculate **sample standard deviation**. ```js -const standardDeviation = (arr, val) => +const standardDeviation = arr => Math.sqrt( arr.reduce((acc, val) => acc.concat(Math.pow(val - arr.reduce((acc, val) => acc + val, 0) / arr.length, 2)), []) .reduce((acc, val) => acc + val, 0) From b844b0f765dbdb69783d544a39f067bb708f8744 Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 23:41:28 +1100 Subject: [PATCH 04/41] Add usePopulation flag param --- snippets/standard-deviation.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index 0ae7f56af..a2b9a64fe 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -3,15 +3,15 @@ Use `Array.reduce()` to calculate the mean of the values, the variance of the values, and the sum of the variance of the values to determine the standard deviation of an array of numbers. -NOTE: This is **population standard deviation**. Use `/ (arr.length - 1)` at the end to -calculate **sample standard deviation**. +Since there are two types of standard deviation, population and sample, you can use a flag to switch to population (sample is default). ```js -const standardDeviation = arr => +const standardDeviation = (arr, usePopulation) => Math.sqrt( arr.reduce((acc, val) => acc.concat(Math.pow(val - arr.reduce((acc, val) => acc + val, 0) / arr.length, 2)), []) .reduce((acc, val) => acc + val, 0) - / arr.length + / (arr.length - (usePopulation ? 0 : 1)) ); -// standardDeviation([10,2,38,23,38,23,21]) -> 12.298996142875 +// standardDeviation([10,2,38,23,38,23,21]) -> 13.284434142114991 (sample) +// standardDeviation([10,2,38,23,38,23,21], true) -> 12.29899614287479 (population) ``` From 3d7b322a9d9defaf99a3f3f27aec45857697156f Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 23:48:29 +1100 Subject: [PATCH 05/41] Cache the mean --- snippets/standard-deviation.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index a2b9a64fe..b1dd0b9c6 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -6,12 +6,14 @@ of the values to determine the standard deviation of an array of numbers. Since there are two types of standard deviation, population and sample, you can use a flag to switch to population (sample is default). ```js -const standardDeviation = (arr, usePopulation) => - Math.sqrt( - arr.reduce((acc, val) => acc.concat(Math.pow(val - arr.reduce((acc, val) => acc + val, 0) / arr.length, 2)), []) +const standardDeviation = (arr, usePopulation) => { + const mean = arr.reduce((acc, val) => acc + val, 0); + return Math.sqrt( + arr.reduce((acc, val) => acc.concat(Math.pow(val - mean / arr.length, 2)), []) .reduce((acc, val) => acc + val, 0) / (arr.length - (usePopulation ? 0 : 1)) ); + } // standardDeviation([10,2,38,23,38,23,21]) -> 13.284434142114991 (sample) // standardDeviation([10,2,38,23,38,23,21], true) -> 12.29899614287479 (population) ``` From 418069551b528e3855d2a39a6375f70a88b36a2e Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 23:54:33 +1100 Subject: [PATCH 06/41] Update standard-deviation.md --- snippets/standard-deviation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index b1dd0b9c6..3641cbe14 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -7,7 +7,7 @@ Since there are two types of standard deviation, population and sample, you can ```js const standardDeviation = (arr, usePopulation) => { - const mean = arr.reduce((acc, val) => acc + val, 0); + const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length; return Math.sqrt( arr.reduce((acc, val) => acc.concat(Math.pow(val - mean / arr.length, 2)), []) .reduce((acc, val) => acc + val, 0) From 98f56cbf2394b59053f14b4e4ef382c456965128 Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 13 Dec 2017 23:55:08 +1100 Subject: [PATCH 07/41] Update standard-deviation.md --- snippets/standard-deviation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index 3641cbe14..442052761 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -9,7 +9,7 @@ Since there are two types of standard deviation, population and sample, you can const standardDeviation = (arr, usePopulation) => { const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length; return Math.sqrt( - arr.reduce((acc, val) => acc.concat(Math.pow(val - mean / arr.length, 2)), []) + arr.reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), []) .reduce((acc, val) => acc + val, 0) / (arr.length - (usePopulation ? 0 : 1)) ); From 4a661d70f2eec0a2dec891d8fed38bdc5cfeaf55 Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Thu, 14 Dec 2017 00:10:56 +0330 Subject: [PATCH 08/41] add sleep function - making delays in async functions --- snippets/sleep.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 snippets/sleep.md diff --git a/snippets/sleep.md b/snippets/sleep.md new file mode 100644 index 000000000..4a7e8916b --- /dev/null +++ b/snippets/sleep.md @@ -0,0 +1,12 @@ +### Sleep + +If you have an async function and you want to delay executing part of it. you can put your async function to sleep(in miliseconds). + +```js +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); +// async function sleepyWork() { +// console.log('I\'m going to sleep for 1 second.'); +// await sleep(1000); +// console.log('I woke up after 1 second.'); +// } +``` From e8d9acae9a3d9dd07a4e06716ceb63a2dc137a37 Mon Sep 17 00:00:00 2001 From: Elder Henrique Souza Date: Wed, 13 Dec 2017 18:49:12 -0200 Subject: [PATCH 09/41] group by Tried to reproduce the groupBy behaviour from the lodash lib. https://lodash.com/docs/4.17.4#groupBy --- snippets/group-by | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 snippets/group-by diff --git a/snippets/group-by b/snippets/group-by new file mode 100644 index 000000000..53383db3a --- /dev/null +++ b/snippets/group-by @@ -0,0 +1,16 @@ +### Group by + +Passing an array of values, a function or a property name thats going to be run against each value in the array, +returns an object where the keys are the mapped results and the values is an array of the original values that generated the same results. + +```js +const groupBy = (values, fn) => { + return (typeof fn === 'function' ? values.map(fn) : values.map((val) => val[fn])) + .reduce((acc, val, i) => { + acc[val] = acc[val] === undefined ? [values[i]] : acc[val].concat(values[i]); + return acc; + }, {}); +} +// groupBy([6.1, 4.2, 6.3], Math.floor) -> {4: [4.2], 6: [6.1, 6.3]} +// groupBy(['one', 'two', 'three'], 'length') -> {3: ['one', 'two'], 5: ['three']} +``` From 8c6059098d19e25d8b415180848af40e4c648bdb Mon Sep 17 00:00:00 2001 From: Darren Scerri Date: Wed, 13 Dec 2017 22:56:36 +0100 Subject: [PATCH 10/41] Remove duplicate snippets. Simplify implementation --- README.md | 24 +++++------------------- snippets/randomize-order-of-array.md | 8 -------- snippets/shuffle-array-values.md | 12 ------------ snippets/shuffle-array.md | 8 ++++++++ 4 files changed, 13 insertions(+), 39 deletions(-) delete mode 100644 snippets/randomize-order-of-array.md delete mode 100644 snippets/shuffle-array-values.md create mode 100644 snippets/shuffle-array.md diff --git a/README.md b/README.md index b77013e78..7317a259d 100644 --- a/README.md +++ b/README.md @@ -50,13 +50,12 @@ * [Promisify](#promisify) * [Random integer in range](#random-integer-in-range) * [Random number in range](#random-number-in-range) -* [Randomize order of array](#randomize-order-of-array) * [Redirect to url](#redirect-to-url) * [Reverse a string](#reverse-a-string) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Run promises in series](#run-promises-in-series) * [Scroll to top](#scroll-to-top) -* [Shuffle array values](#shuffle-array-values) +* [Shuffle array](#shuffle-array) * [Similarity between arrays](#similarity-between-arrays) * [Sort characters in string (alphabetical)](#sort-characters-in-string-alphabetical) * [Sum of array of numbers](#sum-of-array-of-numbers) @@ -510,15 +509,6 @@ const randomInRange = (min, max) => Math.random() * (max - min) + min; // randomInRange(2,10) -> 6.0211363285087005 ``` -### Randomize order of array - -Use `Array.sort()` to reorder elements, utilizing `Math.random()` to randomize the sorting. - -```js -const randomizeOrder = arr => arr.sort( (a,b) => Math.random() >= 0.5 ? -1 : 1); -// randomizeOrder([1,2,3]) -> [1,3,2] -``` - ### Redirect to URL Use `window.location.href` or `window.location.replace()` to redirect to `url`. @@ -575,17 +565,13 @@ const scrollToTop = _ => { // scrollToTop() ``` -### Shuffle array values +### Shuffle array -Create an array of random values by using `Array.map()` and `Math.random()`. -Use `Array.sort()` to sort the elements of the original array based on the random values. +Use `Array.sort()` to reorder elements, using `Math.random()` in the comparator. ```js -const shuffle = arr => { - let r = arr.map(Math.random); - return arr.sort((a,b) => r[a] - r[b]); -} -// shuffle([1,2,3]) -> [2, 1, 3] +const shuffle = arr => arr.sort(() => Math.random() - 0.5); +// shuffle([1,2,3]) -> [2,3,1] ``` ### Similarity between arrays diff --git a/snippets/randomize-order-of-array.md b/snippets/randomize-order-of-array.md deleted file mode 100644 index d65aaf444..000000000 --- a/snippets/randomize-order-of-array.md +++ /dev/null @@ -1,8 +0,0 @@ -### Randomize order of array - -Use `Array.sort()` to reorder elements, utilizing `Math.random()` to randomize the sorting. - -```js -const randomizeOrder = arr => arr.sort( (a,b) => Math.random() >= 0.5 ? -1 : 1); -// randomizeOrder([1,2,3]) -> [1,3,2] -``` diff --git a/snippets/shuffle-array-values.md b/snippets/shuffle-array-values.md deleted file mode 100644 index a140bd647..000000000 --- a/snippets/shuffle-array-values.md +++ /dev/null @@ -1,12 +0,0 @@ -### Shuffle array values - -Create an array of random values by using `Array.map()` and `Math.random()`. -Use `Array.sort()` to sort the elements of the original array based on the random values. - -```js -const shuffle = arr => { - let r = arr.map(Math.random); - return arr.sort((a,b) => r[a] - r[b]); -} -// shuffle([1,2,3]) -> [2, 1, 3] -``` diff --git a/snippets/shuffle-array.md b/snippets/shuffle-array.md new file mode 100644 index 000000000..6266f9ecf --- /dev/null +++ b/snippets/shuffle-array.md @@ -0,0 +1,8 @@ +### Shuffle array + +Use `Array.sort()` to reorder elements, using `Math.random()` in the comparator. + +```js +const shuffle = arr => arr.sort(() => Math.random() - 0.5); +// shuffle([1,2,3]) -> [2,3,1] +``` From 2b34ee4ba92f13c0b0d4f653e8e2fde7f1ca2601 Mon Sep 17 00:00:00 2001 From: Darren Scerri Date: Thu, 14 Dec 2017 01:11:38 +0100 Subject: [PATCH 11/41] Fix names and links --- README.md | 6 +++--- snippets/array-difference.md | 2 +- snippets/array-intersection.md | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ddd428064..7ccd59b50 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ * [Random integer in range](#random-integer-in-range) * [Random number in range](#random-number-in-range) * [Randomize order of array](#randomize-order-of-array) -* [Redirect to URL](#redirect-to-url) +* [Redirect to url](#redirect-to-url) * [Reverse a string](#reverse-a-string) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Run promises in series](#run-promises-in-series) @@ -89,7 +89,7 @@ const anagrams = str => { // anagrams('abc') -> ['abc','acb','bac','bca','cab','cba'] ``` -### Array difference (complement) +### Array difference Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values not contained in `b`. @@ -98,7 +98,7 @@ const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has // difference([1,2,3], [1,2]) -> [3] ``` -### Array intersection (Common values between two arrays) +### Array intersection Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values contained in `b`. diff --git a/snippets/array-difference.md b/snippets/array-difference.md index 74e518f5c..46469b1d6 100644 --- a/snippets/array-difference.md +++ b/snippets/array-difference.md @@ -1,4 +1,4 @@ -### Array difference (complement) +### Array difference Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values not contained in `b`. diff --git a/snippets/array-intersection.md b/snippets/array-intersection.md index 909c4312d..87c42378b 100644 --- a/snippets/array-intersection.md +++ b/snippets/array-intersection.md @@ -1,4 +1,4 @@ -### Array intersection (Common values between two arrays) +### Array intersection Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values contained in `b`. From 5fbfa9422c7fc29ac20e0a6b58bfc4f2e63d440f Mon Sep 17 00:00:00 2001 From: Robert Mennell Date: Wed, 13 Dec 2017 21:20:36 -0800 Subject: [PATCH 12/41] Update README.md Remove the Question mark as per https://github.com/Chalarangelo/30-seconds-of-code/issues/95 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ddd428064..3dc91bb5e 100644 --- a/README.md +++ b/README.md @@ -705,7 +705,7 @@ Pass `location.search` as the argument to apply to the current `url`. ```js const getUrlParameters = url => - url.match(/([^?=&]+)(=([^&]*))?/g).reduce( + url.match(/([^?=&]+)(=([^&]*))/g).reduce( (a, v) => (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1), a), {} ); // getUrlParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'} From e421b1d49d92c1a95a30fe6fa7b157ea4a3426dd Mon Sep 17 00:00:00 2001 From: panshao <1016432619@qq.com> Date: Thu, 14 Dec 2017 14:09:46 +0800 Subject: [PATCH 13/41] Update README.md use Array.from instead of Array.apply --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ddd428064..46f4ff8e9 100644 --- a/README.md +++ b/README.md @@ -185,14 +185,14 @@ const palindrome = str => { ### Chunk array -Use `Array.apply()` to create a new array, that fits the number of chunks that will be produced. -Use `Array.map()` to map each element of the new array to a chunk the length of `size`. +Use `Array.from(arrayLike[, mapFn[, thisArg]])` to create a new array, that fits the number of chunks that will be produced. +Use `mapFn` to map each element of the new array to a chunk the length of `size`. If the original array can't be split evenly, the final chunk will contain the remaining elements. ```js const chunk = (arr, size) => - Array.apply(null, {length: Math.ceil(arr.length / size)}).map((v, i) => arr.slice(i * size, i * size + size)); -// chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],5] + Array.from({length: Math.ceil(arr.length / size)}, (v, i) => arr.slice(i * size, i * size + size)); + // chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],5] ``` ### Count occurrences of a value in array From 81f7e8e9ed5564859b9a33813a7b371ec3cede1e Mon Sep 17 00:00:00 2001 From: Huseyin Sekmenoglu Date: Thu, 14 Dec 2017 10:57:17 +0300 Subject: [PATCH 14/41] Create validate-email.md --- snippets/validate-email.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 snippets/validate-email.md diff --git a/snippets/validate-email.md b/snippets/validate-email.md new file mode 100644 index 000000000..57a8afd2a --- /dev/null +++ b/snippets/validate-email.md @@ -0,0 +1,10 @@ +### Validate Email + + Regex is taken from https://stackoverflow.com/questions/46155/how-to-validate-email-address-in-javascript + Returns `true` if email is valid, `false` if not. + + ```js + const validateEmail = str => /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(str); + // isemail(mymail@gmail.com) -> true + ``` + From 494189089e7c760da4bde2e65c6d5ed8f81793c3 Mon Sep 17 00:00:00 2001 From: Robert Mennell Date: Wed, 13 Dec 2017 21:20:36 -0800 Subject: [PATCH 15/41] Revert "Update README.md" This reverts commit 682f172645a7b16f640f966f25b02de0cd12f55b. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3dc91bb5e..ddd428064 100644 --- a/README.md +++ b/README.md @@ -705,7 +705,7 @@ Pass `location.search` as the argument to apply to the current `url`. ```js const getUrlParameters = url => - url.match(/([^?=&]+)(=([^&]*))/g).reduce( + url.match(/([^?=&]+)(=([^&]*))?/g).reduce( (a, v) => (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1), a), {} ); // getUrlParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'} From e6bfe6606f9ef20468d667cadd7a478bcf80cede Mon Sep 17 00:00:00 2001 From: Robert Mennell Date: Thu, 14 Dec 2017 00:23:14 -0800 Subject: [PATCH 16/41] Fix: remove one or more quantifier from Match to properly pull out parts --- snippets/URL-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/URL-parameters.md b/snippets/URL-parameters.md index 742e7ffbd..f78c774d8 100644 --- a/snippets/URL-parameters.md +++ b/snippets/URL-parameters.md @@ -5,7 +5,7 @@ Pass `location.search` as the argument to apply to the current `url`. ```js const getUrlParameters = url => - url.match(/([^?=&]+)(=([^&]*))?/g).reduce( + url.match(/([^?=&]+)(=([^&]*))/g).reduce( (a, v) => (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1), a), {} ); // getUrlParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'} From 3fae1fa34a953bf262f02fc9ed69df7ac34d4553 Mon Sep 17 00:00:00 2001 From: King Date: Thu, 14 Dec 2017 03:24:05 -0500 Subject: [PATCH 17/41] update description of head & ran npm run build-list --- README.md | 4 ++-- snippets/head-of-list.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 458bf5611..c26a5677d 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ * [Random integer in range](#random-integer-in-range) * [Random number in range](#random-number-in-range) * [Randomize order of array](#randomize-order-of-array) -* [Redirect to URL](#redirect-to-url) +* [Redirect to url](#redirect-to-url) * [Reverse a string](#reverse-a-string) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Run promises in series](#run-promises-in-series) @@ -395,7 +395,7 @@ const hammingDistance = (num1, num2) => ### Head of list -Return `arr[0]`. +Use `arr[0]` to return the first element of the passed array. ```js const head = arr => arr[0]; diff --git a/snippets/head-of-list.md b/snippets/head-of-list.md index 31dc5fbaf..c2e0d5a3a 100644 --- a/snippets/head-of-list.md +++ b/snippets/head-of-list.md @@ -1,6 +1,6 @@ ### Head of list -Return `arr[0]`. +Use `arr[0]` to return the first element of the passed array. ```js const head = arr => arr[0]; From 3ce9c180c59c35a0446c1296cf9072ebf93e5b60 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 10:26:08 +0200 Subject: [PATCH 18/41] Build README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a125ed5c6..850d944cf 100644 --- a/README.md +++ b/README.md @@ -732,7 +732,7 @@ Pass `location.search` as the argument to apply to the current `url`. ```js const getUrlParameters = url => - url.match(/([^?=&]+)(=([^&]*))?/g).reduce( + url.match(/([^?=&]+)(=([^&]*))/g).reduce( (a, v) => (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1), a), {} ); // getUrlParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'} From 80af3e7c6d06b9baa743b477226b675b4391880e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 10:28:09 +0200 Subject: [PATCH 19/41] Build README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index af1cd5ccb..4dbfe200a 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ * [Random integer in range](#random-integer-in-range) * [Random number in range](#random-number-in-range) * [Randomize order of array](#randomize-order-of-array) -* [Redirect to url](#redirect-to-url) +* [Redirect to URL](#redirect-to-url) * [Reverse a string](#reverse-a-string) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Run promises in series](#run-promises-in-series) From e98fd5fe3ab0aa76168d5898ca51d813c5f67df5 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 10:32:59 +0200 Subject: [PATCH 20/41] Build README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 476f862a9..88d89840c 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ * [Random integer in range](#random-integer-in-range) * [Random number in range](#random-number-in-range) * [Randomize order of array](#randomize-order-of-array) -* [Redirect to url](#redirect-to-url) +* [Redirect to URL](#redirect-to-url) * [Reverse a string](#reverse-a-string) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Run promises in series](#run-promises-in-series) From 9980dbac0f596e99e08853057e88a4d49f0caa1d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 10:34:29 +0200 Subject: [PATCH 21/41] Update sleep.md --- snippets/sleep.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/sleep.md b/snippets/sleep.md index 4a7e8916b..c64ed3917 100644 --- a/snippets/sleep.md +++ b/snippets/sleep.md @@ -1,6 +1,6 @@ ### Sleep -If you have an async function and you want to delay executing part of it. you can put your async function to sleep(in miliseconds). +Delay executing part of an `async` function, by putting it to sleep, returning a `Promise`. ```js const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); From 27c02041fd0ed0afc685d5d771cd31d440689afa Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 10:35:58 +0200 Subject: [PATCH 22/41] Build README --- README.md | 16 ++++++++++++++++ snippets/sleep.md | 12 +++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 88d89840c..8b9c31e13 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ * [Scroll to top](#scroll-to-top) * [Shuffle array values](#shuffle-array-values) * [Similarity between arrays](#similarity-between-arrays) +* [Sleep](#sleep) * [Sort characters in string (alphabetical)](#sort-characters-in-string-alphabetical) * [Sum of array of numbers](#sum-of-array-of-numbers) * [Swap values of two variables](#swap-values-of-two-variables) @@ -667,6 +668,21 @@ const similarity = (arr, values) => arr.filter(v => values.includes(v)); // similarity([1,2,3], [1,2,4]) -> [1,2] ``` +### Sleep + +Delay executing part of an `async` function, by putting it to sleep, returning a `Promise`. + +```js +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); +/* +async function sleepyWork() { + console.log('I\'m going to sleep for 1 second.'); + await sleep(1000); + console.log('I woke up after 1 second.'); +} +*/ +``` + ### Sort characters in string (alphabetical) Split the string using `split('')`, `Array.sort()` utilizing `localeCompare()`, recombine using `join('')`. diff --git a/snippets/sleep.md b/snippets/sleep.md index c64ed3917..e2a58d0bd 100644 --- a/snippets/sleep.md +++ b/snippets/sleep.md @@ -4,9 +4,11 @@ Delay executing part of an `async` function, by putting it to sleep, returning a ```js const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); -// async function sleepyWork() { -// console.log('I\'m going to sleep for 1 second.'); -// await sleep(1000); -// console.log('I woke up after 1 second.'); -// } +/* +async function sleepyWork() { + console.log('I\'m going to sleep for 1 second.'); + await sleep(1000); + console.log('I woke up after 1 second.'); +} +*/ ``` From 755425f5562215404aef5cd59a9d00eeb16246de Mon Sep 17 00:00:00 2001 From: King Date: Thu, 14 Dec 2017 03:44:45 -0500 Subject: [PATCH 23/41] ran npm run build-list & update intial-of-list description --- README.md | 4 ++-- snippets/initial-of-list.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8b9c31e13..f370a585a 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ * [Random integer in range](#random-integer-in-range) * [Random number in range](#random-number-in-range) * [Randomize order of array](#randomize-order-of-array) -* [Redirect to URL](#redirect-to-url) +* [Redirect to url](#redirect-to-url) * [Reverse a string](#reverse-a-string) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Run promises in series](#run-promises-in-series) @@ -405,7 +405,7 @@ const head = arr => arr[0]; ### Initial of list -Return `arr.slice(0,-1)`. +Use `arr.slice(0,-1)`to return all but the last element of the array. ```js const initial = arr => arr.slice(0, -1); diff --git a/snippets/initial-of-list.md b/snippets/initial-of-list.md index a222f2306..1f967f804 100644 --- a/snippets/initial-of-list.md +++ b/snippets/initial-of-list.md @@ -1,6 +1,6 @@ ### Initial of list -Return `arr.slice(0,-1)`. +Use `arr.slice(0,-1)`to return all but the last element of the array. ```js const initial = arr => arr.slice(0, -1); From 91f393207df999ed0ef1cf715f08426d570bd426 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 10:45:09 +0200 Subject: [PATCH 24/41] Update group-by Shortened code. It now also follows the styleguide more precisely. Improved description. --- snippets/group-by | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/snippets/group-by b/snippets/group-by index 53383db3a..e6df1a2a7 100644 --- a/snippets/group-by +++ b/snippets/group-by @@ -1,16 +1,14 @@ ### Group by -Passing an array of values, a function or a property name thats going to be run against each value in the array, -returns an object where the keys are the mapped results and the values is an array of the original values that generated the same results. +Use `Array.map()` to map the values of an array to a function or property name. +Use `Array.reduce()` to create an object, where the keys are produced from the mapped results. ```js -const groupBy = (values, fn) => { - return (typeof fn === 'function' ? values.map(fn) : values.map((val) => val[fn])) +const groupBy = (arr, func) => + (typeof func === 'function' ? arr.map(func) : arr.map(val => val[func])) .reduce((acc, val, i) => { - acc[val] = acc[val] === undefined ? [values[i]] : acc[val].concat(values[i]); - return acc; + acc[val] = acc[val] === undefined ? [arr[i]] : acc[val].concat(arr[i]); return acc; }, {}); -} // groupBy([6.1, 4.2, 6.3], Math.floor) -> {4: [4.2], 6: [6.1, 6.3]} // groupBy(['one', 'two', 'three'], 'length') -> {3: ['one', 'two'], 5: ['three']} ``` From 2afdce90ee3d55ce14ce9a8d8e0bb7614e1accc3 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 10:46:46 +0200 Subject: [PATCH 25/41] Build README --- README.md | 16 ++++++++++++++++ snippets/{group-by => group-by.md} | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) rename snippets/{group-by => group-by.md} (94%) diff --git a/README.md b/README.md index 8b9c31e13..522f36584 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ * [Get native type of value](#get-native-type-of-value) * [Get scroll position](#get-scroll-position) * [Greatest common divisor (GCD)](#greatest-common-divisor-gcd) +* [Group by](#group-by) * [Hamming distance](#hamming-distance) * [Head of list](#head-of-list) * [Initial of list](#initial-of-list) @@ -383,6 +384,21 @@ const gcd = (x, y) => !y ? x : gcd(y, x % y); // gcd (8, 36) -> 4 ``` +### Group by + +Use `Array.map()` to map the values of an array to a function or property name. +Use `Array.reduce()` to create an object, where the keys are produced from the mapped results. + +```js +const groupBy = (arr, func) => + (typeof func === 'function' ? arr.map(func) : arr.map(val => val[func])) + .reduce((acc, val, i) => { + acc[val] = acc[val] === undefined ? [arr[i]] : acc[val].concat(arr[i]); return acc; + }, {}); +// groupBy([6.1, 4.2, 6.3], Math.floor) -> {4: [4.2], 6: [6.1, 6.3]} +// groupBy(['one', 'two', 'three'], 'length') -> {3: ['one', 'two'], 5: ['three']} +``` + ### Hamming distance Use XOR operator (`^`) to find the bit difference between the two numbers, convert to binary string using `toString(2)`. diff --git a/snippets/group-by b/snippets/group-by.md similarity index 94% rename from snippets/group-by rename to snippets/group-by.md index e6df1a2a7..ab833c9d2 100644 --- a/snippets/group-by +++ b/snippets/group-by.md @@ -4,7 +4,7 @@ Use `Array.map()` to map the values of an array to a function or property name. Use `Array.reduce()` to create an object, where the keys are produced from the mapped results. ```js -const groupBy = (arr, func) => +const groupBy = (arr, func) => (typeof func === 'function' ? arr.map(func) : arr.map(val => val[func])) .reduce((acc, val, i) => { acc[val] = acc[val] === undefined ? [arr[i]] : acc[val].concat(arr[i]); return acc; From 2123d8d50a72b26003f1d8525426c84265cab871 Mon Sep 17 00:00:00 2001 From: King Date: Thu, 14 Dec 2017 03:49:18 -0500 Subject: [PATCH 26/41] update last-of-list & ran npm run build-list --- README.md | 4 ++-- snippets/last-of-list.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8b9c31e13..d267e205a 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ * [Random integer in range](#random-integer-in-range) * [Random number in range](#random-number-in-range) * [Randomize order of array](#randomize-order-of-array) -* [Redirect to URL](#redirect-to-url) +* [Redirect to url](#redirect-to-url) * [Reverse a string](#reverse-a-string) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Run promises in series](#run-promises-in-series) @@ -435,7 +435,7 @@ const initializeArray = (n, value = 0) => Array(n).fill(value); ### Last of list -Return `arr.slice(-1)[0]`. +Use `arr.slice(-1)[0]` to get the last element of the given array. ```js const last = arr => arr.slice(-1)[0]; diff --git a/snippets/last-of-list.md b/snippets/last-of-list.md index 16955f201..d27b0b35e 100644 --- a/snippets/last-of-list.md +++ b/snippets/last-of-list.md @@ -1,6 +1,6 @@ ### Last of list -Return `arr.slice(-1)[0]`. +Use `arr.slice(-1)[0]` to get the last element of the given array. ```js const last = arr => arr.slice(-1)[0]; From 3b0fa08fdd5052e15759c92263218449d0d1b5f7 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 10:59:20 +0200 Subject: [PATCH 27/41] Build README --- README.md | 8 ++++---- snippets/chunk-array.md | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e3d9727df..dd66eebff 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ * [Random integer in range](#random-integer-in-range) * [Random number in range](#random-number-in-range) * [Randomize order of array](#randomize-order-of-array) -* [Redirect to url](#redirect-to-url) +* [Redirect to URL](#redirect-to-url) * [Reverse a string](#reverse-a-string) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Run promises in series](#run-promises-in-series) @@ -189,14 +189,14 @@ const palindrome = str => { ### Chunk array -Use `Array.from(arrayLike[, mapFn[, thisArg]])` to create a new array, that fits the number of chunks that will be produced. -Use `mapFn` to map each element of the new array to a chunk the length of `size`. +Use `Array.from()` to create a new array, that fits the number of chunks that will be produced. +Use `Array.slice()` to map each element of the new array to a chunk the length of `size`. If the original array can't be split evenly, the final chunk will contain the remaining elements. ```js const chunk = (arr, size) => Array.from({length: Math.ceil(arr.length / size)}, (v, i) => arr.slice(i * size, i * size + size)); - // chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],5] +// chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],5] ``` ### Compact diff --git a/snippets/chunk-array.md b/snippets/chunk-array.md index de6c5a568..c14bf269d 100644 --- a/snippets/chunk-array.md +++ b/snippets/chunk-array.md @@ -1,11 +1,11 @@ ### Chunk array -Use `Array.apply()` to create a new array, that fits the number of chunks that will be produced. -Use `Array.map()` to map each element of the new array to a chunk the length of `size`. +Use `Array.from()` to create a new array, that fits the number of chunks that will be produced. +Use `Array.slice()` to map each element of the new array to a chunk the length of `size`. If the original array can't be split evenly, the final chunk will contain the remaining elements. ```js const chunk = (arr, size) => - Array.apply(null, {length: Math.ceil(arr.length / size)}).map((v, i) => arr.slice(i * size, i * size + size)); + Array.from({length: Math.ceil(arr.length / size)}, (v, i) => arr.slice(i * size, i * size + size)); // chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],5] ``` From f8d55631c6a5d9e4790d30347b45d675c1b0e10c Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 11:10:47 +0200 Subject: [PATCH 28/41] Merge pull request #88 from darrenscerri/shuffle --- README.md | 7 +++---- snippets/randomize-order-of-array.md | 8 -------- snippets/shuffle-array-values.md | 12 ------------ snippets/shuffle-array.md | 8 ++++++++ 4 files changed, 11 insertions(+), 24 deletions(-) delete mode 100644 snippets/randomize-order-of-array.md delete mode 100644 snippets/shuffle-array-values.md create mode 100644 snippets/shuffle-array.md diff --git a/README.md b/README.md index dd66eebff..efc81a9de 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Run promises in series](#run-promises-in-series) * [Scroll to top](#scroll-to-top) -* [Shuffle array values](#shuffle-array-values) +* [Shuffle array](#shuffle-array) * [Similarity between arrays](#similarity-between-arrays) * [Sleep](#sleep) * [Sort characters in string (alphabetical)](#sort-characters-in-string-alphabetical) @@ -662,10 +662,9 @@ const scrollToTop = _ => { // scrollToTop() ``` -### Shuffle array values +### Shuffle array -Create an array of random values by using `Array.map()` and `Math.random()`. -Use `Array.sort()` to sort the elements of the original array based on the random values. +Use `Array.sort()` to reorder elements, using `Math.random()` in the comparator. ```js const shuffle = arr => { diff --git a/snippets/randomize-order-of-array.md b/snippets/randomize-order-of-array.md deleted file mode 100644 index 456a00607..000000000 --- a/snippets/randomize-order-of-array.md +++ /dev/null @@ -1,8 +0,0 @@ -### Randomize order of array - -Use `Array.sort()` to reorder elements, utilizing `Math.random()` to randomize the sorting. - -```js -const randomizeOrder = arr => arr.sort((a, b) => Math.random() >= 0.5 ? -1 : 1); -// randomizeOrder([1,2,3]) -> [1,3,2] -``` diff --git a/snippets/shuffle-array-values.md b/snippets/shuffle-array-values.md deleted file mode 100644 index ab3698a31..000000000 --- a/snippets/shuffle-array-values.md +++ /dev/null @@ -1,12 +0,0 @@ -### Shuffle array values - -Create an array of random values by using `Array.map()` and `Math.random()`. -Use `Array.sort()` to sort the elements of the original array based on the random values. - -```js -const shuffle = arr => { - let r = arr.map(Math.random); - return arr.sort((a, b) => r[a] - r[b]); -}; -// shuffle([1,2,3]) -> [2, 1, 3] -``` diff --git a/snippets/shuffle-array.md b/snippets/shuffle-array.md new file mode 100644 index 000000000..6266f9ecf --- /dev/null +++ b/snippets/shuffle-array.md @@ -0,0 +1,8 @@ +### Shuffle array + +Use `Array.sort()` to reorder elements, using `Math.random()` in the comparator. + +```js +const shuffle = arr => arr.sort(() => Math.random() - 0.5); +// shuffle([1,2,3]) -> [2,3,1] +``` From a528b7f7093c748d7cedf87d0338a9d850753bad Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 11:15:49 +0200 Subject: [PATCH 29/41] Build README --- README.md | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index efc81a9de..db026b6c2 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,6 @@ * [Promisify](#promisify) * [Random integer in range](#random-integer-in-range) * [Random number in range](#random-number-in-range) -* [Randomize order of array](#randomize-order-of-array) * [Redirect to URL](#redirect-to-url) * [Reverse a string](#reverse-a-string) * [RGB to hexadecimal](#rgb-to-hexadecimal) @@ -597,15 +596,6 @@ const randomInRange = (min, max) => Math.random() * (max - min) + min; // randomInRange(2,10) -> 6.0211363285087005 ``` -### Randomize order of array - -Use `Array.sort()` to reorder elements, utilizing `Math.random()` to randomize the sorting. - -```js -const randomizeOrder = arr => arr.sort((a, b) => Math.random() >= 0.5 ? -1 : 1); -// randomizeOrder([1,2,3]) -> [1,3,2] -``` - ### Redirect to URL Use `window.location.href` or `window.location.replace()` to redirect to `url`. @@ -667,11 +657,8 @@ const scrollToTop = _ => { Use `Array.sort()` to reorder elements, using `Math.random()` in the comparator. ```js -const shuffle = arr => { - let r = arr.map(Math.random); - return arr.sort((a, b) => r[a] - r[b]); -}; -// shuffle([1,2,3]) -> [2, 1, 3] +const shuffle = arr => arr.sort(() => Math.random() - 0.5); +// shuffle([1,2,3]) -> [2,3,1] ``` ### Similarity between arrays From 66702ee4764080e9a355076b430603ff26168d56 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 11:23:21 +0200 Subject: [PATCH 30/41] Update validate-email.md --- snippets/validate-email.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/snippets/validate-email.md b/snippets/validate-email.md index 57a8afd2a..b4037d4aa 100644 --- a/snippets/validate-email.md +++ b/snippets/validate-email.md @@ -1,10 +1,11 @@ -### Validate Email +### Validate email - Regex is taken from https://stackoverflow.com/questions/46155/how-to-validate-email-address-in-javascript - Returns `true` if email is valid, `false` if not. +Use a regular experssion to check if the email is valid. +Returns `true` if email is valid, `false` if not. ```js - const validateEmail = str => /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(str); +const validateEmail = str => + /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(str); // isemail(mymail@gmail.com) -> true ``` From bede528c5265c304f14f0064030b310f40ac9536 Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 14 Dec 2017 20:24:07 +1100 Subject: [PATCH 31/41] Use explicit default value --- snippets/standard-deviation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index 442052761..f07cb4f4c 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -6,7 +6,7 @@ of the values to determine the standard deviation of an array of numbers. Since there are two types of standard deviation, population and sample, you can use a flag to switch to population (sample is default). ```js -const standardDeviation = (arr, usePopulation) => { +const standardDeviation = (arr, usePopulation = false) => { const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length; return Math.sqrt( arr.reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), []) From 3fa2d41ff33ec1a0e9f13b81c757356e0a9cc938 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 11:24:46 +0200 Subject: [PATCH 32/41] Build README --- README.md | 12 ++++++++++++ snippets/validate-email.md | 13 ++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index db026b6c2..befac0394 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ * [Unique values of array](#unique-values-of-array) * [URL parameters](#url-parameters) * [UUID generator](#uuid-generator) +* [Validate email](#validate-email) * [Validate number](#validate-number) * [Value or default](#value-or-default) @@ -768,6 +769,17 @@ const uuid = _ => // uuid() -> '7982fcfe-5721-4632-bede-6000885be57d' ``` +### Validate email + +Use a regular experssion to check if the email is valid. +Returns `true` if email is valid, `false` if not. + +```js +const validateEmail = str => + /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(str); +// validateEmail(mymail@gmail.com) -> true +``` + ### Validate number Use `!isNaN` in combination with `parseFloat()` to check if the argument is a number. diff --git a/snippets/validate-email.md b/snippets/validate-email.md index b4037d4aa..d83a41904 100644 --- a/snippets/validate-email.md +++ b/snippets/validate-email.md @@ -1,11 +1,10 @@ ### Validate email - + Use a regular experssion to check if the email is valid. Returns `true` if email is valid, `false` if not. - - ```js -const validateEmail = str => + +```js +const validateEmail = str => /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(str); - // isemail(mymail@gmail.com) -> true - ``` - +// validateEmail(mymail@gmail.com) -> true +``` From 2c30c0afdefb9fc194bd0142d2734e24ec6dc689 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 11:29:20 +0200 Subject: [PATCH 33/41] Update standard-deviation.md --- snippets/standard-deviation.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/snippets/standard-deviation.md b/snippets/standard-deviation.md index f07cb4f4c..e559972bb 100644 --- a/snippets/standard-deviation.md +++ b/snippets/standard-deviation.md @@ -1,17 +1,15 @@ ### Standard deviation -Use `Array.reduce()` to calculate the mean of the values, the variance of the values, and the sum of the variance -of the values to determine the standard deviation of an array of numbers. - -Since there are two types of standard deviation, population and sample, you can use a flag to switch to population (sample is default). +Use `Array.reduce()` to calculate the mean, variance and the sum of the variance of the values, the variance of the values, then +determine the standard deviation. +You can omit the second argument to get the sample standard deviation or set it to `true` to get the population standard deviation. ```js const standardDeviation = (arr, usePopulation = false) => { const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length; return Math.sqrt( arr.reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), []) - .reduce((acc, val) => acc + val, 0) - / (arr.length - (usePopulation ? 0 : 1)) + .reduce((acc, val) => acc + val, 0) / (arr.length - (usePopulation ? 0 : 1)) ); } // standardDeviation([10,2,38,23,38,23,21]) -> 13.284434142114991 (sample) From 0f775ba97b25ca1b3e3ec0d4c1b76b7569da2941 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 11:30:00 +0200 Subject: [PATCH 34/41] Build README --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index befac0394..d0393e21a 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ * [Similarity between arrays](#similarity-between-arrays) * [Sleep](#sleep) * [Sort characters in string (alphabetical)](#sort-characters-in-string-alphabetical) +* [Standard deviation](#standard-deviation) * [Sum of array of numbers](#sum-of-array-of-numbers) * [Swap values of two variables](#swap-values-of-two-variables) * [Tail of list](#tail-of-list) @@ -696,6 +697,24 @@ const sortCharactersInString = str => // sortCharactersInString('cabbage') -> 'aabbceg' ``` +### Standard deviation + +Use `Array.reduce()` to calculate the mean, variance and the sum of the variance of the values, the variance of the values, then +determine the standard deviation. +You can omit the second argument to get the sample standard deviation or set it to `true` to get the population standard deviation. + +```js +const standardDeviation = (arr, usePopulation = false) => { + const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length; + return Math.sqrt( + arr.reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), []) + .reduce((acc, val) => acc + val, 0) / (arr.length - (usePopulation ? 0 : 1)) + ); + } +// standardDeviation([10,2,38,23,38,23,21]) -> 13.284434142114991 (sample) +// standardDeviation([10,2,38,23,38,23,21], true) -> 12.29899614287479 (population) +``` + ### Sum of array of numbers Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`. From 8ca776aa03843deca3dfc5c6fd18f89259762b2f Mon Sep 17 00:00:00 2001 From: King Date: Thu, 14 Dec 2017 04:35:14 -0500 Subject: [PATCH 35/41] ran npm run build-list & add take.md --- README.md | 14 +++++++++++++- snippets/take.md | 10 ++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 snippets/take.md diff --git a/README.md b/README.md index d0393e21a..3abae27c6 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ * [Promisify](#promisify) * [Random integer in range](#random-integer-in-range) * [Random number in range](#random-number-in-range) -* [Redirect to URL](#redirect-to-url) +* [Redirect to url](#redirect-to-url) * [Reverse a string](#reverse-a-string) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Run promises in series](#run-promises-in-series) @@ -70,6 +70,7 @@ * [Sum of array of numbers](#sum-of-array-of-numbers) * [Swap values of two variables](#swap-values-of-two-variables) * [Tail of list](#tail-of-list) +* [Take](#take) * [Truncate a string](#truncate-a-string) * [Unique values of array](#unique-values-of-array) * [URL parameters](#url-parameters) @@ -743,6 +744,17 @@ const tail = arr => arr.length > 1 ? arr.slice(1) : arr; // tail([1]) -> [1] ``` +### Take + +Use `.slice()` to create a slice of the array with n elements taken from the beginning. + +```js +const take = (arr, n) => n === undefined ? arr.slice(0, 1) : arr.slice(0, n); + +// take([1, 2, 3], 5) -> [1, 2, 3] +// take([1, 2, 3], 0) -> [] +``` + ### Truncate a String Determine if the string's `length` is greater than `num`. diff --git a/snippets/take.md b/snippets/take.md new file mode 100644 index 000000000..1cc7e77fe --- /dev/null +++ b/snippets/take.md @@ -0,0 +1,10 @@ +### Take + +Use `.slice()` to create a slice of the array with n elements taken from the beginning. + +```js +const take = (arr, n) => n === undefined ? arr.slice(0, 1) : arr.slice(0, n); + +// take([1, 2, 3], 5) -> [1, 2, 3] +// take([1, 2, 3], 0) -> [] +``` From 236616efea4cbc88a0bde56b186addc4d19ce78b Mon Sep 17 00:00:00 2001 From: King Date: Thu, 14 Dec 2017 04:46:16 -0500 Subject: [PATCH 36/41] refactor take --- README.md | 2 +- snippets/take.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3abae27c6..4a1ea0529 100644 --- a/README.md +++ b/README.md @@ -749,7 +749,7 @@ const tail = arr => arr.length > 1 ? arr.slice(1) : arr; Use `.slice()` to create a slice of the array with n elements taken from the beginning. ```js -const take = (arr, n) => n === undefined ? arr.slice(0, 1) : arr.slice(0, n); +const take = (arr, n = 1) => arr.slice(0, n); // take([1, 2, 3], 5) -> [1, 2, 3] // take([1, 2, 3], 0) -> [] diff --git a/snippets/take.md b/snippets/take.md index 1cc7e77fe..f597728ea 100644 --- a/snippets/take.md +++ b/snippets/take.md @@ -3,7 +3,7 @@ Use `.slice()` to create a slice of the array with n elements taken from the beginning. ```js -const take = (arr, n) => n === undefined ? arr.slice(0, 1) : arr.slice(0, n); +const take = (arr, n = 1) => arr.slice(0, n); // take([1, 2, 3], 5) -> [1, 2, 3] // take([1, 2, 3], 0) -> [] From db28e1ee28385d8946e09ebdcf6f30da1b0b7507 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 11:50:52 +0200 Subject: [PATCH 37/41] Update take.md --- snippets/take.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/take.md b/snippets/take.md index f597728ea..80142031d 100644 --- a/snippets/take.md +++ b/snippets/take.md @@ -1,6 +1,6 @@ ### Take -Use `.slice()` to create a slice of the array with n elements taken from the beginning. +Use `Array.slice()` to create a slice of the array with `n` elements taken from the beginning. ```js const take = (arr, n = 1) => arr.slice(0, n); From a93268e5969c77c38d9a587056581cb39f2b0835 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 11:51:24 +0200 Subject: [PATCH 38/41] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4a1ea0529..0c9b72501 100644 --- a/README.md +++ b/README.md @@ -746,7 +746,7 @@ const tail = arr => arr.length > 1 ? arr.slice(1) : arr; ### Take -Use `.slice()` to create a slice of the array with n elements taken from the beginning. +Use `Array.slice()` to create a slice of the array with `n` elements taken from the beginning. ```js const take = (arr, n = 1) => arr.slice(0, n); From f7095814f4cfa4b4c278caa7dec3be7383c3ebc3 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 11:55:45 +0200 Subject: [PATCH 39/41] Build README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0c9b72501..1015e7c72 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ * [Promisify](#promisify) * [Random integer in range](#random-integer-in-range) * [Random number in range](#random-number-in-range) -* [Redirect to url](#redirect-to-url) +* [Redirect to URL](#redirect-to-url) * [Reverse a string](#reverse-a-string) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Run promises in series](#run-promises-in-series) From 9ebbe62fb97a06d53df8014e196a2ef0e4d7f124 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 12:16:25 +0200 Subject: [PATCH 40/41] Update concat.md Improved description a little. --- snippets/concat.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/concat.md b/snippets/concat.md index 0f6c8bcb1..677bc8871 100644 --- a/snippets/concat.md +++ b/snippets/concat.md @@ -1,6 +1,6 @@ ### Concat -Creates a new array concatenating array with any additional arrays and/or values `args` using `Array.concat()`. +Use `Array.concat()` to concatenate and array with any additional arrays and/or values, specified in `args`. ```js const ArrayConcat = (arr, ...args) => arr.concat(...args); From 67761753118ebf460a4a0fb1dcaa336c7abce129 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 12:17:11 +0200 Subject: [PATCH 41/41] Update and rename concat.md to array-concatenation.md --- snippets/{concat.md => array-concatenation.md} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename snippets/{concat.md => array-concatenation.md} (90%) diff --git a/snippets/concat.md b/snippets/array-concatenation.md similarity index 90% rename from snippets/concat.md rename to snippets/array-concatenation.md index 677bc8871..39d978d6e 100644 --- a/snippets/concat.md +++ b/snippets/array-concatenation.md @@ -1,4 +1,4 @@ -### Concat +### Array concatenation Use `Array.concat()` to concatenate and array with any additional arrays and/or values, specified in `args`.