Created
October 2, 2014 04:17
-
-
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
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 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