Skip to content

Find the Longest Word in a String

Islam Ibakaev edited this page Apr 26, 2016 · 1 revision

Details

Return the length of the longest word in the provided sentence.

Your response should be a number.

Elegant solution

function findLongestWord(str) {
  return Math.max.apply(null, str.split(' ').map(function(el) {return el.length;}));
}

findLongestWord('The quick brown fox jumped over the lazy dog'); // returns 6

Clone this wiki locally