Longest word length in a sentence

Posted on April 30, 2020 by Ted Martin

I recently had a task that involved finding the word of the greatest length in a string. Think about how terribly tedious such a task would be manually. Gratefully computers can do the heavy lifting for us.

Here is the algorithm I came up with:

function findLongestWordLength(string) {
  let wordsLengthArray = []

  let stringToWordsArray = string.split(" ")

  stringToWordsArray.forEach(word => {
    wordsLengthArray.push(word.length)
  })

  return Math.max(...wordsLengthArray)
}

findLongestWordLength("Tempus fugit memento mori!");

Share post

0 comments

There are no comments yet.

Add a new comment