From 1d8d2d812c858a836555c31bbf1c2cb2ed37cdb9 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Mon, 1 Jan 2018 15:48:40 +0530 Subject: [PATCH] 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 ```