Added longestString()

This commit is contained in:
Enzo Volkmann
2018-01-07 23:05:55 +01:00
parent 34561e2183
commit 58d9211d25

15
snippets/longestString.md Normal file
View File

@ -0,0 +1,15 @@
### longestString
Takes an array of strings and returns the longestString one.
Uses the [rest operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters)
to handle arrays as well as an indefinite amount of single arguments.
```js
const longestString = (...strings) => [...strings].reduce((a, b) => a.length > b.length ? a : b);
```
```js
longestString('this', 'is', 'a', 'testcase') // 'testcase'
longestString(['a', 'ab', 'abc']) // 'abc'
```