This commit is contained in:
Rohit Tanwar
2018-01-01 19:13:01 +05:30
2 changed files with 15 additions and 7 deletions

View File

@ -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

View File

@ -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
```