File size: 493 Bytes
ae4627d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | var coderbyte = coderbyte || {};
// Version 1: A regular expression and a for-loop with an artificial termiantion.
coderbyte.getLongestWord = function(string) {
var sanitizedSentence = string.replace(/[^a-z]+/gi, " "),
words = sanitizedSentence.split(" "),
longestWord = "",
i,
len;
for (i = 0, len = words.length; i < len; i++) {
if (words[i].length > longestWord.length) {
longestWord = words[i];
}
}
return longestWord;
}; |