Merge branch 'master' of https://github.com/kriadmin/30-seconds-of-code
This commit is contained in:
@ -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
|
||||
|
||||
@ -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
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user