Skip to content

Instantly share code, notes, and snippets.

@ian128K
Created October 2, 2014 04:17
Show Gist options
  • Save ian128K/320d855b4aab5b8bcaab to your computer and use it in GitHub Desktop.
Save ian128K/320d855b4aab5b8bcaab to your computer and use it in GitHub Desktop.
Function takes an input string and returns the longest word in the string
function LongestWord(sen) {
if(typeof sen = "undefined") {
return "You have to pass a sentence in as a string.";
} else {
var words = sen.split(" ");
if(words.length = 1) {
return words[0];
} else {
var largest = 0;
var output = "";
var length = words.length;
for(var i = 0; i < length; i++) {
if(words[i].length > largest) {
largest = words[i].length;
}
if(words[i].length === largest) {
output = words[i];
}
}
return output;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment