DEV Community

Discussion on: Breaking Down JavaScript Solutions To Common Algorithmic Questions (Part 1)

Collapse
 
qm3ster profile image
Mihail Malo

I'd use this for Longest Word. It's one less iteration, and can be easily modified to return the word.

function findLongestWordLength(str) {
  const arrOfWords = str.split(/\s+/)
  const longestWord = arrOfWords.reduce((a, b) => b.length > a.length ? b : a)
  return longestWord
}