Created
June 9, 2018 17:44
-
-
Save samolabams/2dc850be823663e7eb1414037591cf24 to your computer and use it in GitHub Desktop.
Find the longest word in a sentence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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