diff --git a/snippets/join.md b/snippets/join.md index f951070e5..476af7b15 100644 --- a/snippets/join.md +++ b/snippets/join.md @@ -1,13 +1,21 @@ ### 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 ) => { - 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 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 ```