Skip to content

Instantly share code, notes, and snippets.

@denismcdonald
Last active May 23, 2018 03:31
Show Gist options
  • Save denismcdonald/a75a747acecf008796af39727f0195e2 to your computer and use it in GitHub Desktop.
Save denismcdonald/a75a747acecf008796af39727f0195e2 to your computer and use it in GitHub Desktop.
Find the longest word in a sentence and return word length (JavaScript)
function findLongestWord(str) {
var splitString = str.split(" ");
var longestString = splitString[0];
for (var i = 0; i < splitString.length; i++) {
if (splitString[i].length > longestString.length) {
longestString = splitString[i];
}
}
return longestString.length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment