From ba3cf0ea2ae362aadf4bb82e18802c6fb7816923 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Tue, 2 Jan 2018 16:16:47 +0530 Subject: [PATCH 01/21] add RPN snippets --- snippets/infixToPostFix.md | 24 ++++++++++++++++++++++++ snippets/postfixToInfix.md | 29 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 snippets/infixToPostFix.md create mode 100644 snippets/postfixToInfix.md diff --git a/snippets/infixToPostFix.md b/snippets/infixToPostFix.md new file mode 100644 index 000000000..3fd859e37 --- /dev/null +++ b/snippets/infixToPostFix.md @@ -0,0 +1,24 @@ +const infix = expr => { + let rpn = '' + let solve = expr.replace(/\^/g,'**').match(/([0-9]+|[\+\(\)\/\-]|\*+)/g).filter(el => !/\s+/.test(el) && el !== '') + let stack = [] + let precedence = {'**':5,'/':4,'*':4,'+':3,'-':3} + solve.forEach(symbol => { + if(!isNaN(parseFloat(symbol)) && isFinite(symbol)){ + rpn += symbol + ' ' + } + else if (Object.keys(precedence).includes(symbol)) { + while(((precedence[symbol] < precedence[stack[stack.length-1]]) || ((precedence[symbol] == precedence[stack[stack.length-1]]) && symbol !== "**"))&&(stack[stack.length - 1] !== '(')) { + rpn += stack.pop() + ' ' + } + stack.push(symbol) + } + else if(symbol === '('){stack.push(symbol)} + else if(symbol === ')'){while (stack[stack.length - 1] !== '('){rpn += stack.pop() + ' '; if (stack.length === 0){throw `Mismatched parantheses`}} stack.pop()} + else {throw `${symbol} is not a recognized symbol`} + }) + while(stack.length !== 0){ + rpn += stack.pop() + ' ' + } + return rpn +} diff --git a/snippets/postfixToInfix.md b/snippets/postfixToInfix.md new file mode 100644 index 000000000..c54bd44f0 --- /dev/null +++ b/snippets/postfixToInfix.md @@ -0,0 +1,29 @@ +const postFix = RPN => { + let convert = RPN.replace(/\^/g,'**').split(/\s+/g).filter(el => !/\s+/.test(el) && el !== '') + let stack = [] + let result = [] + let friends = {"+" : ["+","-","*","/"],"-":[],"/":["*"],"*":["/","*"],"**":["+","-","*","/"]} + convert.forEach(symbol => { + if(!isNaN(parseFloat(symbol)) && isFinite(symbol)){ + result.push(symbol) + } + else if (Object.keys(friends).includes(symbol)) { + a = result.pop() + b = result.pop() + if(stack.length !==0){ + if(friends[symbol].includes(stack.pop())){ + result.push(`${b} ${symbol} ${a}`) + stack.push(symbol) + } + else{ + result.push(`(${b}) ${symbol} ${a}`) + stack.push(symbol) + } + } + else {result.push(`${b} ${symbol} ${a}`);stack.push(symbol)} + } + else throw `${symbol} is not a recognized symbol` + }) + if(result.length === 1) return result.pop() + else throw `${RPN} is not a correct RPN` +} From 78c29a1ddb76b7eb76536b0c5c15d31e66193a02 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Tue, 2 Jan 2018 16:39:26 +0530 Subject: [PATCH 02/21] fixed typos --- snippets/RPNSolver.md | 24 ++++++++++++++++++++++++ snippets/infixToPostFix.md | 11 ++++++++++- snippets/postfixToInfix.md | 11 ++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 snippets/RPNSolver.md diff --git a/snippets/RPNSolver.md b/snippets/RPNSolver.md new file mode 100644 index 000000000..2cfb59be5 --- /dev/null +++ b/snippets/RPNSolver.md @@ -0,0 +1,24 @@ +### RPNSolver + +Solves the given reverse polish notation + +``` js +const RPNSolver = RPN => { + const operators = {'*' : (a,b) => a * b, '+' : (a,b) => a + b, '-' : (a,b) => a - b, '/' : (a,b) => a / b, '**': (a,b) => a ** b} + let [stack,solve] = [[],RPN.replace(/\^/g,'**').split(/\s+/g).filter(el => !/\s+/.test(el) && el !== '')] + solve.forEach(symbol => { + if(!isNaN(parseFloat(symbol)) && isFinite(symbol)) {stack.push(symbol)} + else if (Object.keys(operators).includes(symbol)) { + let [a,b] = [stack.pop(),stack.pop()] + stack.push(operators[symbol](parseFloat(b),parseFloat(a))) + } else { throw `${symbol} is not a recognized symbol` } + } +) + if(stack.length === 1) return stack.pop() + else throw `${RPN} is not a proper RPN. Please check it and try again` +} +``` + +```js + +``` diff --git a/snippets/infixToPostFix.md b/snippets/infixToPostFix.md index 3fd859e37..b05496977 100644 --- a/snippets/infixToPostFix.md +++ b/snippets/infixToPostFix.md @@ -1,4 +1,9 @@ -const infix = expr => { +### infixToPostfix + +Works perfectly! + +```js +const infixToPostfix = expr => { let rpn = '' let solve = expr.replace(/\^/g,'**').match(/([0-9]+|[\+\(\)\/\-]|\*+)/g).filter(el => !/\s+/.test(el) && el !== '') let stack = [] @@ -22,3 +27,7 @@ const infix = expr => { } return rpn } +``` +```js + +``` diff --git a/snippets/postfixToInfix.md b/snippets/postfixToInfix.md index c54bd44f0..19e191c6b 100644 --- a/snippets/postfixToInfix.md +++ b/snippets/postfixToInfix.md @@ -1,4 +1,9 @@ -const postFix = RPN => { +### postfixToInfix + + This one has few errors and does not work properply + +```js +const postfixToInfix = RPN => { let convert = RPN.replace(/\^/g,'**').split(/\s+/g).filter(el => !/\s+/.test(el) && el !== '') let stack = [] let result = [] @@ -27,3 +32,7 @@ const postFix = RPN => { if(result.length === 1) return result.pop() else throw `${RPN} is not a correct RPN` } +``` +```js + +``` From a5fc517f2b732c2a492c6af89be6488ceee0cbd2 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Tue, 2 Jan 2018 16:40:19 +0530 Subject: [PATCH 03/21] fixed typos --- snippets/RPNSolver.md | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 snippets/RPNSolver.md diff --git a/snippets/RPNSolver.md b/snippets/RPNSolver.md deleted file mode 100644 index 2cfb59be5..000000000 --- a/snippets/RPNSolver.md +++ /dev/null @@ -1,24 +0,0 @@ -### RPNSolver - -Solves the given reverse polish notation - -``` js -const RPNSolver = RPN => { - const operators = {'*' : (a,b) => a * b, '+' : (a,b) => a + b, '-' : (a,b) => a - b, '/' : (a,b) => a / b, '**': (a,b) => a ** b} - let [stack,solve] = [[],RPN.replace(/\^/g,'**').split(/\s+/g).filter(el => !/\s+/.test(el) && el !== '')] - solve.forEach(symbol => { - if(!isNaN(parseFloat(symbol)) && isFinite(symbol)) {stack.push(symbol)} - else if (Object.keys(operators).includes(symbol)) { - let [a,b] = [stack.pop(),stack.pop()] - stack.push(operators[symbol](parseFloat(b),parseFloat(a))) - } else { throw `${symbol} is not a recognized symbol` } - } -) - if(stack.length === 1) return stack.pop() - else throw `${RPN} is not a proper RPN. Please check it and try again` -} -``` - -```js - -``` From abbfd00707e6a5c26cd0b5438af4d5d988975889 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Tue, 2 Jan 2018 21:14:44 +0530 Subject: [PATCH 04/21] fixed typos --- snippets/infixToPostFix.md | 2 +- snippets/postfixToInfix.md | 37 ++++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/snippets/infixToPostFix.md b/snippets/infixToPostFix.md index b05496977..84371ddbc 100644 --- a/snippets/infixToPostFix.md +++ b/snippets/infixToPostFix.md @@ -1,6 +1,6 @@ ### infixToPostfix -Works perfectly! +Works perfectly! ```js const infixToPostfix = expr => { diff --git a/snippets/postfixToInfix.md b/snippets/postfixToInfix.md index 19e191c6b..52d1a89bb 100644 --- a/snippets/postfixToInfix.md +++ b/snippets/postfixToInfix.md @@ -1,32 +1,35 @@ ### postfixToInfix - This one has few errors and does not work properply -```js +``` js const postfixToInfix = RPN => { let convert = RPN.replace(/\^/g,'**').split(/\s+/g).filter(el => !/\s+/.test(el) && el !== '') let stack = [] let result = [] - let friends = {"+" : ["+","-","*","/"],"-":[],"/":["*"],"*":["/","*"],"**":["+","-","*","/"]} + let precedence = {null : 4 ,'**':3 ,'/' : 2,'*': 2,'+':1,'-':1 } convert.forEach(symbol => { + let stra,strb if(!isNaN(parseFloat(symbol)) && isFinite(symbol)){ result.push(symbol) + stack.push(null) } - else if (Object.keys(friends).includes(symbol)) { - a = result.pop() - b = result.pop() - if(stack.length !==0){ - if(friends[symbol].includes(stack.pop())){ - result.push(`${b} ${symbol} ${a}`) - stack.push(symbol) - } - else{ - result.push(`(${b}) ${symbol} ${a}`) - stack.push(symbol) - } + else if (Object.keys(precedence).includes(symbol)) { + let [a,b,opa,opb] = [result.pop(),result.pop(),stack.pop(),stack.pop()] + if(precedence[opb] < precedence[symbol]) { + strb = `(${b})` } - else {result.push(`${b} ${symbol} ${a}`);stack.push(symbol)} - } + else{ + strb = `${b}` + } + if((precedence[opa] < precedence[symbol]) || ((precedence[opa] === precedence[symbol]) && ["/","-"].includes(symbol) )){ + stra = `(${a})` + } + else { + stra = `${a}` + } + result.push(strb +symbol + stra) + stack.push(symbol) + } else throw `${symbol} is not a recognized symbol` }) if(result.length === 1) return result.pop() From 9062c161b7b7828be10b50e9179a5b07db959bd5 Mon Sep 17 00:00:00 2001 From: David Wu Date: Tue, 2 Jan 2018 19:12:46 +0100 Subject: [PATCH 05/21] Add ProductHunt --- static-parts/README-start.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/static-parts/README-start.md b/static-parts/README-start.md index 02cd09f4f..8effbc1c3 100644 --- a/static-parts/README-start.md +++ b/static-parts/README-start.md @@ -1,8 +1,11 @@ ![Logo](/logo.png) -# 30 seconds of code +# 30 seconds of code [![ProductHunt](https://img.shields.io/badge/producthunt-vote-orange.svg)](https://www.producthunt.com/posts/30-seconds-of-code) [![License](https://img.shields.io/badge/license-CC0--1.0-blue.svg)](https://github.com/Chalarangelo/30-seconds-of-code/blob/master/LICENSE) [![Gitter chat](https://img.shields.io/badge/chat-on%20gitter-4FB999.svg)](https://gitter.im/30-seconds-of-code/Lobby) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) [![Travis Build](https://travis-ci.org/Chalarangelo/30-seconds-of-code.svg?branch=master)](https://travis-ci.org/Chalarangelo/30-seconds-of-code) [![Insight.io](https://img.shields.io/badge/insight.io-Ready-brightgreen.svg)](https://insight.io/github.com/Chalarangelo/30-seconds-of-code/tree/master/?source=0) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg)](https://github.com/Flet/semistandard) +### _Vote for this project on [Producthunt](https://www.producthunt.com/posts/30-seconds-of-code)_ + + > Curated collection of useful Javascript snippets that you can understand in 30 seconds or less. From b3f6f8e7e630bb01dee4ce229aa125184d0659bf Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Wed, 3 Jan 2018 08:48:29 +0530 Subject: [PATCH 06/21] add minN and maxN --- snippets/maxN.md | 19 +++++++++++++++++++ snippets/minN.md | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 snippets/maxN.md create mode 100644 snippets/minN.md diff --git a/snippets/maxN.md b/snippets/maxN.md new file mode 100644 index 000000000..8efc46112 --- /dev/null +++ b/snippets/maxN.md @@ -0,0 +1,19 @@ +### maxN + +Returns the `n` maximum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in descending order). + +Sort's the array's shallow copy in descending order and returns the first n elements + +Skip the second argument to get a single element(in the form of a array) +``` js +const maxN = (arr,n = 1) => { + let rra = [...arr].sort((a,b) => b-a) + return rra.splice(0,n) +} +``` + +``` js +maxN([1,2,3]); // [3] +maxN([1,2,3],2); // [3,2] +maxN([1,2,3],4); // [3,2,1] +``` diff --git a/snippets/minN.md b/snippets/minN.md new file mode 100644 index 000000000..0f40f1d36 --- /dev/null +++ b/snippets/minN.md @@ -0,0 +1,18 @@ +### minN + +Returns the `n` minimum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in ascending order). + +Sort's the array's shallow copy in ascending order and returns the first n elements + +Skip the second argument to get a single element(in the form of a array) +``` js +const minN = (arr,n = 1) => { + let rra = [...arr].sort((a,b) => a-b) + return rra.splice(0,n) +} +``` +``` js +maxN([1,2,3]); // [1] +maxN([1,2,3],2); // [1,2] +maxN([1,2,3],4); // [1,2,3] +``` From 94028604f9f209b74010873aed7bb0f8c957e27f Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Wed, 3 Jan 2018 08:51:23 +0530 Subject: [PATCH 07/21] add minN and maxN --- snippets/infixToPostFix.md | 33 ------------------------------ snippets/postfixToInfix.md | 41 -------------------------------------- 2 files changed, 74 deletions(-) delete mode 100644 snippets/infixToPostFix.md delete mode 100644 snippets/postfixToInfix.md diff --git a/snippets/infixToPostFix.md b/snippets/infixToPostFix.md deleted file mode 100644 index 84371ddbc..000000000 --- a/snippets/infixToPostFix.md +++ /dev/null @@ -1,33 +0,0 @@ -### infixToPostfix - -Works perfectly! - -```js -const infixToPostfix = expr => { - let rpn = '' - let solve = expr.replace(/\^/g,'**').match(/([0-9]+|[\+\(\)\/\-]|\*+)/g).filter(el => !/\s+/.test(el) && el !== '') - let stack = [] - let precedence = {'**':5,'/':4,'*':4,'+':3,'-':3} - solve.forEach(symbol => { - if(!isNaN(parseFloat(symbol)) && isFinite(symbol)){ - rpn += symbol + ' ' - } - else if (Object.keys(precedence).includes(symbol)) { - while(((precedence[symbol] < precedence[stack[stack.length-1]]) || ((precedence[symbol] == precedence[stack[stack.length-1]]) && symbol !== "**"))&&(stack[stack.length - 1] !== '(')) { - rpn += stack.pop() + ' ' - } - stack.push(symbol) - } - else if(symbol === '('){stack.push(symbol)} - else if(symbol === ')'){while (stack[stack.length - 1] !== '('){rpn += stack.pop() + ' '; if (stack.length === 0){throw `Mismatched parantheses`}} stack.pop()} - else {throw `${symbol} is not a recognized symbol`} - }) - while(stack.length !== 0){ - rpn += stack.pop() + ' ' - } - return rpn -} -``` -```js - -``` diff --git a/snippets/postfixToInfix.md b/snippets/postfixToInfix.md deleted file mode 100644 index 52d1a89bb..000000000 --- a/snippets/postfixToInfix.md +++ /dev/null @@ -1,41 +0,0 @@ -### postfixToInfix - - -``` js -const postfixToInfix = RPN => { - let convert = RPN.replace(/\^/g,'**').split(/\s+/g).filter(el => !/\s+/.test(el) && el !== '') - let stack = [] - let result = [] - let precedence = {null : 4 ,'**':3 ,'/' : 2,'*': 2,'+':1,'-':1 } - convert.forEach(symbol => { - let stra,strb - if(!isNaN(parseFloat(symbol)) && isFinite(symbol)){ - result.push(symbol) - stack.push(null) - } - else if (Object.keys(precedence).includes(symbol)) { - let [a,b,opa,opb] = [result.pop(),result.pop(),stack.pop(),stack.pop()] - if(precedence[opb] < precedence[symbol]) { - strb = `(${b})` - } - else{ - strb = `${b}` - } - if((precedence[opa] < precedence[symbol]) || ((precedence[opa] === precedence[symbol]) && ["/","-"].includes(symbol) )){ - stra = `(${a})` - } - else { - stra = `${a}` - } - result.push(strb +symbol + stra) - stack.push(symbol) - } - else throw `${symbol} is not a recognized symbol` - }) - if(result.length === 1) return result.pop() - else throw `${RPN} is not a correct RPN` -} -``` -```js - -``` From 857e5083160e49e6b103b30c76d2eb746e0ca0a5 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 08:55:24 +0530 Subject: [PATCH 08/21] Fixed grammar --- snippets/sum.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/sum.md b/snippets/sum.md index c6894b6a8..a69aa3d4f 100644 --- a/snippets/sum.md +++ b/snippets/sum.md @@ -1,6 +1,6 @@ ### sum -Returns the sum of an of two or more numbers/arrays. +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`. From 18981b54af187fe75fa9a650777af3aee15149fb Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 09:03:45 +0530 Subject: [PATCH 09/21] Add the list of supported operator Add the list of supported operators and specify that unary operators are not supported. Also tell that `^` & `**` are the same and are exponential operators(as `^` can be confused to be the xOR symbol) --- snippets/solveRPN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/solveRPN.md b/snippets/solveRPN.md index 099be512a..87a1fee3f 100644 --- a/snippets/solveRPN.md +++ b/snippets/solveRPN.md @@ -1,7 +1,7 @@ ### solveRPN Solves the given mathematical expression in [reverse polish notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation). -Throws appropriate errors if there are unrecognized symbols or the expression is wrong. +Throws appropriate errors if there are unrecognized symbols or the expression is wrong. The valid operators are :- `+`,`-`,`*`,`/`,`^`,`**` (`^`&`**` are the exponential symbols and are same). This snippet does not supports any unary operators. Use a dictionary, `OPERATORS` to specify each operator's matching mathematical operation. Use `String.replace()` with a regular expression to replace `^` with `**`, `String.split()` to tokenize the string and `Array.filter()` to remove empty tokens. From 8454637f9ea9d9eabaf27700dd944f6ba7476f33 Mon Sep 17 00:00:00 2001 From: Travis CI Date: Wed, 3 Jan 2018 06:02:08 +0000 Subject: [PATCH 10/21] Travis build: 880 --- README.md | 2 +- docs/index.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 68e24214e..4591b60e6 100644 --- a/README.md +++ b/README.md @@ -3340,7 +3340,7 @@ standardDeviation([10, 2, 38, 23, 38, 23, 21], true); // 12.29899614287479 (popu ### sum -Returns the sum of an of two or more numbers/arrays. +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`. diff --git a/docs/index.html b/docs/index.html index 92f670e5d..a3e69f2df 100644 --- a/docs/index.html +++ b/docs/index.html @@ -732,7 +732,7 @@ solveRPN('2 3 ^'); //8 };
standardDeviation([10, 2, 38, 23, 38, 23, 21]); // 13.284434142114991 (sample)
 standardDeviation([10, 2, 38, 23, 38, 23, 21], true); // 12.29899614287479 (population)
-

sum

Returns the sum of an 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) => [].concat(...arr).reduce((acc, val) => acc + val, 0);
+

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) => [].concat(...arr).reduce((acc, val) => acc + val, 0);
 
sum([1, 2, 3, 4]); // 10
 

sumPower

Returns the sum of the powers of all the numbers from start to end (both inclusive).

Use Array.fill() to create an array of all the numbers in the target range, Array.map() and the exponent operator (**) to raise them to power and Array.reduce() to add them together. Omit the second argument, power, to use a default power of 2. Omit the third argument, start, to use a default starting value of 1.

const sumPower = (end, power = 2, start = 1) =>
   Array(end + 1 - start)

From bfb21ff71a3c950ddca6013c65e5c003d05e55f3 Mon Sep 17 00:00:00 2001
From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com>
Date: Wed, 3 Jan 2018 11:39:40 +0530
Subject: [PATCH 11/21] Update maxN.md

---
 snippets/maxN.md | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/snippets/maxN.md b/snippets/maxN.md
index 8efc46112..e777b1949 100644
--- a/snippets/maxN.md
+++ b/snippets/maxN.md
@@ -6,10 +6,7 @@ Sort's the array's shallow copy in descending order and returns the first n elem
 
 Skip the second argument to get a single element(in the form of a array)
 ``` js
-const maxN = (arr,n = 1) => {
-  let rra = [...arr].sort((a,b) => b-a)
-  return rra.splice(0,n)
-}
+const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);
 ```
 
 ``` js

From 231ff2331bc7c0966965c17b83d37af4bb9b0f2a Mon Sep 17 00:00:00 2001
From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com>
Date: Wed, 3 Jan 2018 11:40:39 +0530
Subject: [PATCH 12/21] Update minN.md

---
 snippets/minN.md | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/snippets/minN.md b/snippets/minN.md
index 0f40f1d36..d3a7bd13f 100644
--- a/snippets/minN.md
+++ b/snippets/minN.md
@@ -6,13 +6,11 @@ Sort's the array's shallow copy in ascending order and returns the first n eleme
 
 Skip the second argument to get a single element(in the form of a array)
 ``` js
-const minN = (arr,n = 1) => {
-  let rra = [...arr].sort((a,b) => a-b)
-  return rra.splice(0,n)
-}
+const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
+
 ```
 ``` js
-maxN([1,2,3]); // [1]
-maxN([1,2,3],2); // [1,2]
-maxN([1,2,3],4); // [1,2,3]
+minN([1,2,3]); // [1]
+minN([1,2,3],2); // [1,2]
+minN([1,2,3],4); // [1,2,3]
 ```

From f8ee98b77ab4aadedd7d8d64f9f1b1cd4c5c513b Mon Sep 17 00:00:00 2001
From: Travis CI 
Date: Wed, 3 Jan 2018 06:11:28 +0000
Subject: [PATCH 13/21] Travis build: 884

---
 README.md       | 7 +++++--
 docs/index.html | 2 +-
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index 4591b60e6..111abf34c 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,11 @@
 ![Logo](/logo.png)
 
-# 30 seconds of code
+# 30 seconds of code [![ProductHunt](https://img.shields.io/badge/producthunt-vote-orange.svg)](https://www.producthunt.com/posts/30-seconds-of-code)
 [![License](https://img.shields.io/badge/license-CC0--1.0-blue.svg)](https://github.com/Chalarangelo/30-seconds-of-code/blob/master/LICENSE) [![Gitter chat](https://img.shields.io/badge/chat-on%20gitter-4FB999.svg)](https://gitter.im/30-seconds-of-code/Lobby) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) [![Travis Build](https://travis-ci.org/Chalarangelo/30-seconds-of-code.svg?branch=master)](https://travis-ci.org/Chalarangelo/30-seconds-of-code) [![Insight.io](https://img.shields.io/badge/insight.io-Ready-brightgreen.svg)](https://insight.io/github.com/Chalarangelo/30-seconds-of-code/tree/master/?source=0) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg)](https://github.com/Flet/semistandard)
 
+### _Vote for this project on [Producthunt](https://www.producthunt.com/posts/30-seconds-of-code)_
+
+
 > Curated collection of useful Javascript snippets that you can understand in 30 seconds or less.
 
 
@@ -3253,7 +3256,7 @@ round(1.005, 2); // 1.01
 ### solveRPN
 
 Solves the given mathematical expression in [reverse polish notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation).
-Throws appropriate errors if there are unrecognized symbols or the expression is wrong.
+Throws appropriate errors if there are unrecognized symbols or the expression is wrong. The valid operators are :- `+`,`-`,`*`,`/`,`^`,`**` (`^`&`**` are the exponential symbols and are same). This snippet does not supports any unary operators.
 
 Use a dictionary, `OPERATORS` to specify each operator's matching mathematical operation.
 Use `String.replace()` with a regular expression to replace `^` with `**`, `String.split()` to tokenize the string and `Array.filter()` to remove empty tokens.
diff --git a/docs/index.html b/docs/index.html
index a3e69f2df..adf23cfc7 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -690,7 +690,7 @@ median([0, 10, -2, 7]); // 3.5
 
randomNumberInRange(2, 10); // 6.0211363285087005
 

round

Rounds a number to a specified amount of digits.

Use Math.round() and template literals to round the number to the specified number of digits. Omit the second argument, decimals to round to an integer.

const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`);
 
round(1.005, 2); // 1.01
-

solveRPN

Solves the given mathematical expression in reverse polish notation. Throws appropriate errors if there are unrecognized symbols or the expression is wrong.

Use a dictionary, OPERATORS to specify each operator's matching mathematical operation. Use String.replace() with a regular expression to replace ^ with **, String.split() to tokenize the string and Array.filter() to remove empty tokens. Use Array.forEach() to parse each symbol, evaluate it as a numeric value or operator and solve the mathematical expression. Numeric values are converted to floating point numbers and pushed to a stack, while operators are evaluated using the OPERATORS dictionary and pop elements from the stack to apply operations.

const solveRPN = rpn => {
+

solveRPN

Solves the given mathematical expression in reverse polish notation. Throws appropriate errors if there are unrecognized symbols or the expression is wrong. The valid operators are :- +,-,*,/,^,** (^&** are the exponential symbols and are same). This snippet does not supports any unary operators.

Use a dictionary, OPERATORS to specify each operator's matching mathematical operation. Use String.replace() with a regular expression to replace ^ with **, String.split() to tokenize the string and Array.filter() to remove empty tokens. Use Array.forEach() to parse each symbol, evaluate it as a numeric value or operator and solve the mathematical expression. Numeric values are converted to floating point numbers and pushed to a stack, while operators are evaluated using the OPERATORS dictionary and pop elements from the stack to apply operations.

const solveRPN = rpn => {
   const OPERATORS = {
     '*': (a, b) => a * b,
     '+': (a, b) => a + b,

From 1e1e98582e23897ffafd41a3dab98a5f656136ef Mon Sep 17 00:00:00 2001
From: Travis CI 
Date: Wed, 3 Jan 2018 06:17:04 +0000
Subject: [PATCH 14/21] Travis build: 887

---
 README.md        | 51 ++++++++++++++++++++++++++++++++++++++++++++++++
 docs/index.html  | 10 +++++++++-
 snippets/maxN.md | 10 +++++-----
 snippets/minN.md | 11 +++++------
 tag_database     |  2 ++
 5 files changed, 72 insertions(+), 12 deletions(-)

diff --git a/README.md b/README.md
index 111abf34c..8c8977479 100644
--- a/README.md
+++ b/README.md
@@ -286,6 +286,16 @@
 
 
 
+### _Uncategorized_
+
+
+View contents + +* [`maxN`](#maxn) +* [`minN`](#minn) + +
+ --- ## 🔌 Adapter @@ -5130,6 +5140,47 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) +--- + ## _Uncategorized_ + +### maxN + +Returns the `n` maximum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in descending order). + +Sort's the array's shallow copy in descending order and returns the first n elements + +Skip the second argument to get a single element(in the form of a array) +```js +const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n); +``` + +```js +maxN([1, 2, 3]); // [3] +maxN([1, 2, 3], 2); // [3,2] +maxN([1, 2, 3], 4); // [3,2,1] +``` + +
[⬆ back to top](#table-of-contents) + + +### minN + +Returns the `n` minimum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in ascending order). + +Sort's the array's shallow copy in ascending order and returns the first n elements + +Skip the second argument to get a single element(in the form of a array) +```js +const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n); +``` +```js +minN([1, 2, 3]); // [1] +minN([1, 2, 3], 2); // [1,2] +minN([1, 2, 3], 4); // [1,2,3] +``` + +
[⬆ back to top](#table-of-contents) + ## Collaborators diff --git a/docs/index.html b/docs/index.html index adf23cfc7..82eab6aae 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

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

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+    }

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

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -1155,4 +1155,12 @@ console.log(sdbm('age')); // 808122783
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
+

Uncategorized

maxN

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

Sort's the array's shallow copy in descending order and returns the first n elements

Skip the second argument to get a single element(in the form of a 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]
+maxN([1, 2, 3], 4); // [3,2,1]
+

minN

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

Sort's the array's shallow copy in ascending order and returns the first n elements

Skip the second argument to get a single element(in the form of a 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]
+minN([1, 2, 3], 4); // [1,2,3]
 

\ No newline at end of file diff --git a/snippets/maxN.md b/snippets/maxN.md index e777b1949..efc9e5052 100644 --- a/snippets/maxN.md +++ b/snippets/maxN.md @@ -5,12 +5,12 @@ Returns the `n` maximum elements from the provided array. If `n` is greater than Sort's the array's shallow copy in descending order and returns the first n elements Skip the second argument to get a single element(in the form of a array) -``` js +```js const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n); ``` -``` js -maxN([1,2,3]); // [3] -maxN([1,2,3],2); // [3,2] -maxN([1,2,3],4); // [3,2,1] +```js +maxN([1, 2, 3]); // [3] +maxN([1, 2, 3], 2); // [3,2] +maxN([1, 2, 3], 4); // [3,2,1] ``` diff --git a/snippets/minN.md b/snippets/minN.md index d3a7bd13f..2cee5b091 100644 --- a/snippets/minN.md +++ b/snippets/minN.md @@ -5,12 +5,11 @@ Returns the `n` minimum elements from the provided array. If `n` is greater than Sort's the array's shallow copy in ascending order and returns the first n elements Skip the second argument to get a single element(in the form of a array) -``` js +```js const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n); - ``` -``` js -minN([1,2,3]); // [1] -minN([1,2,3],2); // [1,2] -minN([1,2,3],4); // [1,2,3] +```js +minN([1, 2, 3]); // [1] +minN([1, 2, 3], 2); // [1,2] +minN([1, 2, 3], 4); // [1,2,3] ``` diff --git a/tag_database b/tag_database index 540368f1d..a0e099b93 100644 --- a/tag_database +++ b/tag_database @@ -96,9 +96,11 @@ lowercaseKeys:object mapObject:array mask:string max:math +maxN:uncategorized median:math memoize:function min:math +minN:uncategorized negate:logic nthElement:array objectFromPairs:object From 88c8c3596ea5660f3a5078d0cb680272647456d2 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 12:30:55 +0530 Subject: [PATCH 15/21] Update InitializeArrayWithRange.md Update InitializeArrayWithRange.md to include another parameter step --- snippets/initializeArrayWithRange.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md index 22b126ad8..ba68e3b2c 100644 --- a/snippets/initializeArrayWithRange.md +++ b/snippets/initializeArrayWithRange.md @@ -1,16 +1,18 @@ ### initializeArrayWithRange -Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive. +Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive with there common difference `step`. -Use `Array((end + 1) - start)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range. +Use `Array(Math.ceil((end + 1) - start)/2)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range. You can omit `start` to use a default value of `0`. - +You can omit `step` to use a default value of `1`. + ```js -const initializeArrayWithRange = (end, start = 0) => - Array.from({ length: end + 1 - start }).map((v, i) => i + start); +const initializeArrayWithRange = (end, start = 0,step = 1) => + Array(Math.ceil((end + 1 - start)/2)).fill(0).map((v, i) => (i * step) + start); ``` ```js initializeArrayWithRange(5); // [0,1,2,3,4,5] initializeArrayWithRange(7, 3); // [3,4,5,6,7] +initializeArrayWithRange(9,0,2); //[0,2,4,6,8] ``` From 83f6a7f4e7b07fb3f87bb09b002c34b3e52a9971 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 13:11:37 +0530 Subject: [PATCH 16/21] Update initializeArrayWithRange.md --- snippets/initializeArrayWithRange.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md index ba68e3b2c..2b3c323a1 100644 --- a/snippets/initializeArrayWithRange.md +++ b/snippets/initializeArrayWithRange.md @@ -8,7 +8,7 @@ You can omit `step` to use a default value of `1`. ```js const initializeArrayWithRange = (end, start = 0,step = 1) => - Array(Math.ceil((end + 1 - start)/2)).fill(0).map((v, i) => (i * step) + start); + Array.from({length:Math.ceil((end + 1 - start)/2))}).map((v, i) => (i * step) + start); ``` ```js From c137dd5277eaa34d4b348a44766d0f7fdab35742 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 14:38:42 +0530 Subject: [PATCH 17/21] Update initializeArrayWithRange.md --- snippets/initializeArrayWithRange.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md index 2b3c323a1..38945c1b3 100644 --- a/snippets/initializeArrayWithRange.md +++ b/snippets/initializeArrayWithRange.md @@ -8,7 +8,7 @@ You can omit `step` to use a default value of `1`. ```js const initializeArrayWithRange = (end, start = 0,step = 1) => - Array.from({length:Math.ceil((end + 1 - start)/2))}).map((v, i) => (i * step) + start); + Array.from({length:Math.ceil((end + 2 - start - step)/step)}).map((v, i) => (i * step) + start); ``` ```js From 65aa192008b05a7e7b8c96cd2214ad719a557a6f Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 3 Jan 2018 13:38:12 +0200 Subject: [PATCH 18/21] Update readme badges --- static-parts/README-start.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/static-parts/README-start.md b/static-parts/README-start.md index 8effbc1c3..a3a94d299 100644 --- a/static-parts/README-start.md +++ b/static-parts/README-start.md @@ -1,9 +1,8 @@ ![Logo](/logo.png) -# 30 seconds of code [![ProductHunt](https://img.shields.io/badge/producthunt-vote-orange.svg)](https://www.producthunt.com/posts/30-seconds-of-code) -[![License](https://img.shields.io/badge/license-CC0--1.0-blue.svg)](https://github.com/Chalarangelo/30-seconds-of-code/blob/master/LICENSE) [![Gitter chat](https://img.shields.io/badge/chat-on%20gitter-4FB999.svg)](https://gitter.im/30-seconds-of-code/Lobby) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) [![Travis Build](https://travis-ci.org/Chalarangelo/30-seconds-of-code.svg?branch=master)](https://travis-ci.org/Chalarangelo/30-seconds-of-code) [![Insight.io](https://img.shields.io/badge/insight.io-Ready-brightgreen.svg)](https://insight.io/github.com/Chalarangelo/30-seconds-of-code/tree/master/?source=0) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg)](https://github.com/Flet/semistandard) +# 30 seconds of code -### _Vote for this project on [Producthunt](https://www.producthunt.com/posts/30-seconds-of-code)_ +[![License](https://img.shields.io/badge/license-CC0--1.0-blue.svg)](https://github.com/Chalarangelo/30-seconds-of-code/blob/master/LICENSE) [![Gitter chat](https://img.shields.io/badge/chat-on%20gitter-4FB999.svg)](https://gitter.im/30-seconds-of-code/Lobby) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) [![Travis Build](https://travis-ci.org/Chalarangelo/30-seconds-of-code.svg?branch=master)](https://travis-ci.org/Chalarangelo/30-seconds-of-code) [![Insight.io](https://img.shields.io/badge/insight.io-Ready-brightgreen.svg)](https://insight.io/github.com/Chalarangelo/30-seconds-of-code/tree/master/?source=0) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg)](https://github.com/Flet/semistandard) [![ProductHunt](https://img.shields.io/badge/producthunt-vote-orange.svg)](https://www.producthunt.com/posts/30-seconds-of-code) > Curated collection of useful Javascript snippets that you can understand in 30 seconds or less. From 08c885385509c702e9f83913c03cbfd23b3ed9a6 Mon Sep 17 00:00:00 2001 From: Travis CI Date: Wed, 3 Jan 2018 11:39:18 +0000 Subject: [PATCH 19/21] Travis build: 898 --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8c8977479..53d1142d2 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,8 @@ ![Logo](/logo.png) -# 30 seconds of code [![ProductHunt](https://img.shields.io/badge/producthunt-vote-orange.svg)](https://www.producthunt.com/posts/30-seconds-of-code) -[![License](https://img.shields.io/badge/license-CC0--1.0-blue.svg)](https://github.com/Chalarangelo/30-seconds-of-code/blob/master/LICENSE) [![Gitter chat](https://img.shields.io/badge/chat-on%20gitter-4FB999.svg)](https://gitter.im/30-seconds-of-code/Lobby) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) [![Travis Build](https://travis-ci.org/Chalarangelo/30-seconds-of-code.svg?branch=master)](https://travis-ci.org/Chalarangelo/30-seconds-of-code) [![Insight.io](https://img.shields.io/badge/insight.io-Ready-brightgreen.svg)](https://insight.io/github.com/Chalarangelo/30-seconds-of-code/tree/master/?source=0) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg)](https://github.com/Flet/semistandard) +# 30 seconds of code -### _Vote for this project on [Producthunt](https://www.producthunt.com/posts/30-seconds-of-code)_ +[![License](https://img.shields.io/badge/license-CC0--1.0-blue.svg)](https://github.com/Chalarangelo/30-seconds-of-code/blob/master/LICENSE) [![Gitter chat](https://img.shields.io/badge/chat-on%20gitter-4FB999.svg)](https://gitter.im/30-seconds-of-code/Lobby) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) [![Travis Build](https://travis-ci.org/Chalarangelo/30-seconds-of-code.svg?branch=master)](https://travis-ci.org/Chalarangelo/30-seconds-of-code) [![Insight.io](https://img.shields.io/badge/insight.io-Ready-brightgreen.svg)](https://insight.io/github.com/Chalarangelo/30-seconds-of-code/tree/master/?source=0) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg)](https://github.com/Flet/semistandard) [![ProductHunt](https://img.shields.io/badge/producthunt-vote-orange.svg)](https://www.producthunt.com/posts/30-seconds-of-code) > Curated collection of useful Javascript snippets that you can understand in 30 seconds or less. From 3c4b21ca0f9b8e697fb4076cdd50d4be14880e40 Mon Sep 17 00:00:00 2001 From: David Wu Date: Wed, 3 Jan 2018 12:42:11 +0100 Subject: [PATCH 20/21] Fix end inclusive error --- snippets/initializeArrayWithRange.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md index 38945c1b3..53cf27d4d 100644 --- a/snippets/initializeArrayWithRange.md +++ b/snippets/initializeArrayWithRange.md @@ -8,7 +8,7 @@ You can omit `step` to use a default value of `1`. ```js const initializeArrayWithRange = (end, start = 0,step = 1) => - Array.from({length:Math.ceil((end + 2 - start - step)/step)}).map((v, i) => (i * step) + start); + Array.from({length:Math.ceil((end + 1 - start)/step)}).map((v, i) => (i * step) + start); ``` ```js From 5dfd0602021f28ec4ff427779c627fb2750836da Mon Sep 17 00:00:00 2001 From: David Wu Date: Wed, 3 Jan 2018 12:45:09 +0100 Subject: [PATCH 21/21] Update description --- snippets/initializeArrayWithRange.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md index 53cf27d4d..7c803aed0 100644 --- a/snippets/initializeArrayWithRange.md +++ b/snippets/initializeArrayWithRange.md @@ -2,7 +2,7 @@ Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive with there common difference `step`. -Use `Array(Math.ceil((end + 1) - start)/2)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range. +Use `Array(Math.ceil((end+1-start)/step)` to create an array of the desired length(the amounts of elements is equal to `(end-start)/step` or `(end+1-start)/step` for inclusive end), `Array.map()` to fill with the desired values in a range. You can omit `start` to use a default value of `0`. You can omit `step` to use a default value of `1`.