From 1e6f93ae9c94a3b894cb503f563c4b537c43c4b9 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Tue, 2 Jan 2018 21:37:31 +0530 Subject: [PATCH 01/12] Update --- snippets/postfixToInfix.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/snippets/postfixToInfix.md b/snippets/postfixToInfix.md index 52d1a89bb..6736859e6 100644 --- a/snippets/postfixToInfix.md +++ b/snippets/postfixToInfix.md @@ -4,9 +4,7 @@ ``` 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 } + let [stack,result,precedence] = [[],[],{null : 4 ,'**':3 ,'/' : 2,'*': 2,'+':1,'-':1 }] convert.forEach(symbol => { let stra,strb if(!isNaN(parseFloat(symbol)) && isFinite(symbol)){ From cd1e548255403b93ced33cb76f75a9ae6be1289e Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Tue, 2 Jan 2018 21:53:59 +0530 Subject: [PATCH 02/12] Update infixToPostFix.md --- snippets/infixToPostFix.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/snippets/infixToPostFix.md b/snippets/infixToPostFix.md index 84371ddbc..61ed9f205 100644 --- a/snippets/infixToPostFix.md +++ b/snippets/infixToPostFix.md @@ -1,7 +1,6 @@ ### infixToPostfix -Works perfectly! - +(WARNING)! :- Does not handles `unary` operator ```js const infixToPostfix = expr => { let rpn = '' From c682048cc75f1720b177bfa3c5471dcc0bd6c0fc Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Tue, 2 Jan 2018 21:54:39 +0530 Subject: [PATCH 03/12] Update postfixToInfix.md --- snippets/postfixToInfix.md | 1 + 1 file changed, 1 insertion(+) diff --git a/snippets/postfixToInfix.md b/snippets/postfixToInfix.md index 6736859e6..7d4328ff9 100644 --- a/snippets/postfixToInfix.md +++ b/snippets/postfixToInfix.md @@ -1,5 +1,6 @@ ### postfixToInfix +(WARNING) :- Does not handles `unary` operator well. ``` js const postfixToInfix = RPN => { From 4591b2d264c15c4a34efeadfff3cedb6dcf86e04 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 11:06:38 +0530 Subject: [PATCH 04/12] Update infixToPostFix.md --- snippets/infixToPostFix.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/infixToPostFix.md b/snippets/infixToPostFix.md index 61ed9f205..9a04892ee 100644 --- a/snippets/infixToPostFix.md +++ b/snippets/infixToPostFix.md @@ -4,7 +4,7 @@ ```js const infixToPostfix = expr => { let rpn = '' - let solve = expr.replace(/\^/g,'**').match(/([0-9]+|[\+\(\)\/\-]|\*+)/g).filter(el => !/\s+/.test(el) && el !== '') + let solve = expr.replace(/\^/g,'**')str.replace(/([^\d])\.(\d+)/g, '$10.$2').match(/((\d\.?)+|[\+\(\)\/\-]|\*+)/g).filter(el => !/\s+/.test(el) && el !== '') let stack = [] let precedence = {'**':5,'/':4,'*':4,'+':3,'-':3} solve.forEach(symbol => { From f52b1ced95e47dc1cec4511414a89c6ccf861091 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Sat, 6 Jan 2018 13:14:10 +0530 Subject: [PATCH 05/12] add chunk array --- snippets/chunksArray.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 snippets/chunksArray.md diff --git a/snippets/chunksArray.md b/snippets/chunksArray.md new file mode 100644 index 000000000..da99f3471 --- /dev/null +++ b/snippets/chunksArray.md @@ -0,0 +1,17 @@ +### chunksArray + +Chunks the provided array into the given number of arguments. Can be used with strings too using `Strig.protoype.split()` function. + +``` js +const chunksArray = (arr,num) => { + let array = [] + arr.forEach((el,i) => {if (i <= arr.length - num) array.push(arr.slice(i,i+num))} ) + return array.length ? array : [arr] +} +``` + +```js +chunksArray([1,2,3,4,5],2); //[[1,2],[2,3],[3,4],[4,5]] +chunksArray([1,2,3,4,5],5); //[[1,2,3,4,5]] +chunksArray([1,2,3,4,5],10); //[[1,2,3,4,5]] +``` From 92b6b27350349f29befa48fb83a381f0a34dd02d Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Sat, 6 Jan 2018 13:54:37 +0530 Subject: [PATCH 06/12] indexesOf --- snippets/indexesOf.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 snippets/indexesOf.md diff --git a/snippets/indexesOf.md b/snippets/indexesOf.md new file mode 100644 index 000000000..3b71bbb83 --- /dev/null +++ b/snippets/indexesOf.md @@ -0,0 +1,13 @@ +### indexesOf + +Returns an array of indexes at which the `val` occurs in `arr`. If it occurs only once return the `index` and if it never occurs returns `-1` + +``` js +const indexesOf = (arr, val) => { + let indexes = [], i; + for(i = 0; i < arr.length; i++) + if (arr[i] === val) + indexes.push(i); + return indexes.length === 0 ? -1 : indexes.length === 1 indexes.pop() : indexes +} +``` From 2bc04c38f041b071bfe98722ab0028080c991b5b Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Sat, 6 Jan 2018 13:57:52 +0530 Subject: [PATCH 07/12] indexesOf --- snippets/chunksArray.md | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 snippets/chunksArray.md diff --git a/snippets/chunksArray.md b/snippets/chunksArray.md deleted file mode 100644 index da99f3471..000000000 --- a/snippets/chunksArray.md +++ /dev/null @@ -1,17 +0,0 @@ -### chunksArray - -Chunks the provided array into the given number of arguments. Can be used with strings too using `Strig.protoype.split()` function. - -``` js -const chunksArray = (arr,num) => { - let array = [] - arr.forEach((el,i) => {if (i <= arr.length - num) array.push(arr.slice(i,i+num))} ) - return array.length ? array : [arr] -} -``` - -```js -chunksArray([1,2,3,4,5],2); //[[1,2],[2,3],[3,4],[4,5]] -chunksArray([1,2,3,4,5],5); //[[1,2,3,4,5]] -chunksArray([1,2,3,4,5],10); //[[1,2,3,4,5]] -``` From 6ddc05464cf9935110e2a13de15fca0a67bfa366 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Sat, 6 Jan 2018 14:01:23 +0530 Subject: [PATCH 08/12] Add examples --- snippets/indexesOf.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/snippets/indexesOf.md b/snippets/indexesOf.md index 3b71bbb83..957e60689 100644 --- a/snippets/indexesOf.md +++ b/snippets/indexesOf.md @@ -11,3 +11,9 @@ const indexesOf = (arr, val) => { return indexes.length === 0 ? -1 : indexes.length === 1 indexes.pop() : indexes } ``` +``` js +indexesOf([1,2,3],1); // 0 +indexesOf([1,2,3,1,2,3],1); // [0,3] +indexesOf([1,2,3],4); // -1 +indexesOf([[1,2,3]],[1,2,3]); // -1 (Array.prototype.indexOf()) has the same behaviour +``` From 8ab5b44f222cbf09db176af54ddc0d040c771a97 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Sat, 6 Jan 2018 14:16:21 +0530 Subject: [PATCH 09/12] Update as recommended by @skcat31 --- snippets/indexesOf.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/snippets/indexesOf.md b/snippets/indexesOf.md index 957e60689..8f1eb8690 100644 --- a/snippets/indexesOf.md +++ b/snippets/indexesOf.md @@ -5,15 +5,13 @@ Returns an array of indexes at which the `val` occurs in `arr`. If it occurs onl ``` js const indexesOf = (arr, val) => { let indexes = [], i; - for(i = 0; i < arr.length; i++) - if (arr[i] === val) - indexes.push(i); - return indexes.length === 0 ? -1 : indexes.length === 1 indexes.pop() : indexes + arr.forEach((el,i) => {if(el === val) indexes.push(i)}) + return indexes.length === 0 ? [-1] : indexes } ``` ``` js -indexesOf([1,2,3],1); // 0 +indexesOf([1,2,3],1); // [0] indexesOf([1,2,3,1,2,3],1); // [0,3] -indexesOf([1,2,3],4); // -1 -indexesOf([[1,2,3]],[1,2,3]); // -1 (Array.prototype.indexOf()) has the same behaviour +indexesOf([1,2,3],4); // [-1] +indexesOf([[1,2,3]],[1,2,3]); // [-1] (Array.prototype.indexOf()) has the same behaviour ``` From ca298e5389f5af994ecaf0f5d17fb046f8604c98 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Sat, 6 Jan 2018 15:37:56 +0530 Subject: [PATCH 10/12] Rename indexOfAll --- snippets/{indexesOf.md => indexOfAll.md} | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) rename snippets/{indexesOf.md => indexOfAll.md} (55%) diff --git a/snippets/indexesOf.md b/snippets/indexOfAll.md similarity index 55% rename from snippets/indexesOf.md rename to snippets/indexOfAll.md index 8f1eb8690..f542e1e31 100644 --- a/snippets/indexesOf.md +++ b/snippets/indexOfAll.md @@ -1,17 +1,17 @@ -### indexesOf +### indexOfAll Returns an array of indexes at which the `val` occurs in `arr`. If it occurs only once return the `index` and if it never occurs returns `-1` ``` js -const indexesOf = (arr, val) => { +const indexOfAll = (arr, val) => { let indexes = [], i; arr.forEach((el,i) => {if(el === val) indexes.push(i)}) return indexes.length === 0 ? [-1] : indexes } ``` ``` js -indexesOf([1,2,3],1); // [0] -indexesOf([1,2,3,1,2,3],1); // [0,3] -indexesOf([1,2,3],4); // [-1] -indexesOf([[1,2,3]],[1,2,3]); // [-1] (Array.prototype.indexOf()) has the same behaviour +indexOfAll([1,2,3],1); // [0] +indexOfAll([1,2,3,1,2,3],1); // [0,3] +indexOfAll([1,2,3],4); // [-1] +indexOfAll([[1,2,3]],[1,2,3]); // [-1] (Array.prototype.indexOf()) has the same behaviour ``` From c500de403d06cd9d11a742868ecd59d64e96207f Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Sat, 6 Jan 2018 16:41:42 +0530 Subject: [PATCH 11/12] Update --- snippets/indexOfAll.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/snippets/indexOfAll.md b/snippets/indexOfAll.md index f542e1e31..fb636aecf 100644 --- a/snippets/indexOfAll.md +++ b/snippets/indexOfAll.md @@ -4,10 +4,11 @@ Returns an array of indexes at which the `val` occurs in `arr`. If it occurs onl ``` js const indexOfAll = (arr, val) => { - let indexes = [], i; - arr.forEach((el,i) => {if(el === val) indexes.push(i)}) - return indexes.length === 0 ? [-1] : indexes -} + const indices = []; + arr.forEach((el, i) => el === val && indices.push(i)) + return indices.length ? indices : [-1]; +}; + ``` ``` js indexOfAll([1,2,3],1); // [0] From e05f516240e12cb42e5cba5fd7d85b413b5297fe Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 6 Jan 2018 13:44:51 +0200 Subject: [PATCH 12/12] Update indexOfAll.md --- snippets/indexOfAll.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/snippets/indexOfAll.md b/snippets/indexOfAll.md index fb636aecf..d453e8a5f 100644 --- a/snippets/indexOfAll.md +++ b/snippets/indexOfAll.md @@ -1,6 +1,9 @@ ### indexOfAll -Returns an array of indexes at which the `val` occurs in `arr`. If it occurs only once return the `index` and if it never occurs returns `-1` +Returns all indices of `val` in an array. If `val` never occurs, returns `[-1]`. + +Use `Array.forEach()` to loop over elements and `Array.push()` to store indices for matching elements. +Return `[-1]` if `length` of the array of indices is `0`, otherwise return the array of indices. ``` js const indexOfAll = (arr, val) => { @@ -10,9 +13,8 @@ const indexOfAll = (arr, val) => { }; ``` + ``` js -indexOfAll([1,2,3],1); // [0] indexOfAll([1,2,3,1,2,3],1); // [0,3] indexOfAll([1,2,3],4); // [-1] -indexOfAll([[1,2,3]],[1,2,3]); // [-1] (Array.prototype.indexOf()) has the same behaviour ```