From 7ba9c4f2ca01ad42e660f360ef99ea792b102a2e Mon Sep 17 00:00:00 2001 From: Enzo Volkmann Date: Sun, 7 Jan 2018 23:05:55 +0100 Subject: [PATCH] Added longestString() --- snippets/longestString.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 snippets/longestString.md diff --git a/snippets/longestString.md b/snippets/longestString.md new file mode 100644 index 000000000..c8d02ca4d --- /dev/null +++ b/snippets/longestString.md @@ -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' +```