Skip to content

Instantly share code, notes, and snippets.

@samolabams
Created June 9, 2018 17:44
Show Gist options
  • Save samolabams/2dc850be823663e7eb1414037591cf24 to your computer and use it in GitHub Desktop.
Save samolabams/2dc850be823663e7eb1414037591cf24 to your computer and use it in GitHub Desktop.
Find the longest word in a sentence
function longest(sentence) {
var sentence_words = sentence.split(' ');
if (!Array.isArray(sentence_words) || sentence_words.length === 0) return false;
var longest_word = sentence_words[0];
for (let i=0; i < sentence_words.length; i++) {
if (sentence_words[i].trim().length > longest_word.trim().length) {
longest_word = sentence_words[i];
}
}
return longest_word;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment