Skip to content

Instantly share code, notes, and snippets.

@bflannery
Created December 20, 2016 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bflannery/21b945631a167121702ac2090aecd861 to your computer and use it in GitHub Desktop.
Save bflannery/21b945631a167121702ac2090aecd861 to your computer and use it in GitHub Desktop.
Find the Longest Word in a String
// Return the length of the longest word in the provided sentence.
// Your response should be a number.
function findLongestWord(str) {
var longestWord = str.split(' ').reduce(function(longest, currentWord) {
return currentWord.length > longest.length ? currentWord : longest;
}, "");
return longestWord.length;
}
console.log(findLongestWord("The quick brown fox jumped over the lazy dog"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment