DEV Community

loading...

Discussion on: Keeping Your Code Simple

Collapse
maxschumacher profile image
Max

In my understanding, using the .reduce() function is somewhat misplaced here. You're not looking to reduce all values in an array to one value, but rather look for one element that has certain properties.

Finding things in arrays (and in life) is easier when there is order in search space.

Here's another approach to finding the longest string in an array:

const longestString = arr => arr.sort((a, b) => b.length - a.length)[0];