From acc19aaf4f4127a745fbad2fb936f14b636f010a Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Mon, 1 Jan 2018 15:48:40 +0530 Subject: [PATCH 1/8] add join --- snippets/join.md | 18 ++++++++++++++++++ snippets/sortedIndex.md | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 snippets/join.md diff --git a/snippets/join.md b/snippets/join.md new file mode 100644 index 000000000..fa27f04a5 --- /dev/null +++ b/snippets/join.md @@ -0,0 +1,18 @@ +### join + + + +```js +const join = (arr,start = ',',end = start) => { + return arr.reduce((acc,val,i) => { + return i == arr.length - 2 ? acc + val + end : i == arr.length - 1 ? acc + val : acc + val + start + },'') + } +``` + +```js +join(); // '' +join(['pen','pineapple','apple','pen'],',','&'); //"pen,pineapple,apple&pen" +join(['pen','pineapple','apple','pen'],','); //"pen,pineapple,apple,pen" +join(['pen','pineapple','apple','pen']); //"pen,pineapple,apple,pen" +``` diff --git a/snippets/sortedIndex.md b/snippets/sortedIndex.md index ba9a7599c..1a959f91e 100644 --- a/snippets/sortedIndex.md +++ b/snippets/sortedIndex.md @@ -13,6 +13,6 @@ const sortedIndex = (arr,n) => { ``` ```js -sortedIndex([5,3,2,1], 4); // 1 +sortedIndex([5,3,2,1], 4); //1 sortedIndex([30,50],40); // 1 ``` From 4fbdd2ed760ea815bf52133423a3bbab62f30c13 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Mon, 1 Jan 2018 15:53:26 +0530 Subject: [PATCH 2/8] add examples for join --- snippets/join.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/join.md b/snippets/join.md index fa27f04a5..f951070e5 100644 --- a/snippets/join.md +++ b/snippets/join.md @@ -1,11 +1,11 @@ ### join - +Is like `Array.join()` but with an additional argument of `end`(is equal to `separator` by default) which is used to separate the second to last and last items in the array. Returns `""` when the array is empty and the first item when the length of array is 1. ```js -const join = (arr,start = ',',end = start) => { +const join = (arr,separator = ',',end = separator ) => { return arr.reduce((acc,val,i) => { - return i == arr.length - 2 ? acc + val + end : i == arr.length - 1 ? acc + val : acc + val + start + return i == arr.length - 2 ? acc + val + end : i == arr.length - 1 ? acc + val : acc + val + separator },'') } ``` From 7b90bdddc3aa865f99d67e388b6c494f384047f4 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Mon, 1 Jan 2018 15:59:52 +0530 Subject: [PATCH 3/8] Resolve conflicts --- snippets/sortedIndex.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/sortedIndex.md b/snippets/sortedIndex.md index 8a514a0dd..f83da6ec0 100644 --- a/snippets/sortedIndex.md +++ b/snippets/sortedIndex.md @@ -14,6 +14,6 @@ const sortedIndex = (arr, n) => { ``` ```js -sortedIndex([5,3,2,1], 4); //1 +sortedIndex([5,3,2,1], 4); // 1 sortedIndex([30,50],40); // 1 ``` From ed65e817483d7c7efdb67250bbd56114d149c08b Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Mon, 1 Jan 2018 16:09:29 +0530 Subject: [PATCH 4/8] Update join.md as suggested by @atomiks --- snippets/join.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/snippets/join.md b/snippets/join.md index f951070e5..93f1d0e02 100644 --- a/snippets/join.md +++ b/snippets/join.md @@ -3,11 +3,8 @@ Is like `Array.join()` but with an additional argument of `end`(is equal to `separator` by default) which is used to separate the second to last and last items in the array. Returns `""` when the array is empty and the first item when the length of array is 1. ```js -const join = (arr,separator = ',',end = separator ) => { - return arr.reduce((acc,val,i) => { - return i == arr.length - 2 ? acc + val + end : i == arr.length - 1 ? acc + val : acc + val + separator - },'') - } +const join = (arr, separator = ',', end = separator) => + arr.slice(0, arr.length - 1).join(separator) + end + arr[arr.length - 1] ``` ```js From 23835dbdb984f28eedf51eb1d75867e581a856e9 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Mon, 1 Jan 2018 16:14:48 +0530 Subject: [PATCH 5/8] Remove changes suggested by @atomiks --- snippets/join.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/snippets/join.md b/snippets/join.md index 93f1d0e02..f951070e5 100644 --- a/snippets/join.md +++ b/snippets/join.md @@ -3,8 +3,11 @@ Is like `Array.join()` but with an additional argument of `end`(is equal to `separator` by default) which is used to separate the second to last and last items in the array. Returns `""` when the array is empty and the first item when the length of array is 1. ```js -const join = (arr, separator = ',', end = separator) => - arr.slice(0, arr.length - 1).join(separator) + end + arr[arr.length - 1] +const join = (arr,separator = ',',end = separator ) => { + return arr.reduce((acc,val,i) => { + return i == arr.length - 2 ? acc + val + end : i == arr.length - 1 ? acc + val : acc + val + separator + },'') + } ``` ```js From 4adec91856618b193ab0e3329873f3ac005c9ba1 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Mon, 1 Jan 2018 16:23:31 +0530 Subject: [PATCH 6/8] Update join.md --- snippets/join.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/join.md b/snippets/join.md index f951070e5..b5a847dc9 100644 --- a/snippets/join.md +++ b/snippets/join.md @@ -3,7 +3,7 @@ Is like `Array.join()` but with an additional argument of `end`(is equal to `separator` by default) which is used to separate the second to last and last items in the array. Returns `""` when the array is empty and the first item when the length of array is 1. ```js -const join = (arr,separator = ',',end = separator ) => { +const join = (arr = [],separator = ',',end = separator ) => { return arr.reduce((acc,val,i) => { return i == arr.length - 2 ? acc + val + end : i == arr.length - 1 ? acc + val : acc + val + separator },'') From e58d87b717f5702abfa1dfe525e8f7e72ece0745 Mon Sep 17 00:00:00 2001 From: atomiks Date: Mon, 1 Jan 2018 21:54:31 +1100 Subject: [PATCH 7/8] Update join.md --- snippets/join.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/snippets/join.md b/snippets/join.md index b5a847dc9..de2c45b2c 100644 --- a/snippets/join.md +++ b/snippets/join.md @@ -3,11 +3,15 @@ Is like `Array.join()` but with an additional argument of `end`(is equal to `separator` by default) which is used to separate the second to last and last items in the array. Returns `""` when the array is empty and the first item when the length of array is 1. ```js -const join = (arr = [],separator = ',',end = separator ) => { - return arr.reduce((acc,val,i) => { - return i == arr.length - 2 ? acc + val + end : i == arr.length - 1 ? acc + val : acc + val + separator - },'') - } +const join = (arr = [], separator = ',', end = separator) => + arr.reduce( + (acc, val, i) => + i == arr.length - 2 + ? acc + val + end + : i == arr.length - 1 ? acc + val : acc + val + separator, + '' + ); + ``` ```js From ead17574f79ad3ae01c9bb1b8bcdbad179be1810 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Jan 2018 14:32:21 +0200 Subject: [PATCH 8/8] Update join.md --- snippets/join.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/snippets/join.md b/snippets/join.md index de2c45b2c..476af7b15 100644 --- a/snippets/join.md +++ b/snippets/join.md @@ -1,9 +1,13 @@ ### join -Is like `Array.join()` but with an additional argument of `end`(is equal to `separator` by default) which is used to separate the second to last and last items in the array. Returns `""` when the array is empty and the first item when the length of array is 1. +Joins all elements of an array into a string and returns this string. Uses a separator and an end separator. + +Use `Array.reduce()` to combine elements into a string. +Omit the second argument, `separator`, to use a default separator of `','`. +Omit the third argument, `end`, to use the same value as `separator` by default. ```js -const join = (arr = [], separator = ',', end = separator) => +const join = (arr, separator = ',', end = separator) => arr.reduce( (acc, val, i) => i == arr.length - 2