Last active
September 29, 2015 11:23
-
-
Save waltzaround/b83c0b276d4fa2bdf0b6 to your computer and use it in GitHub Desktop.
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 findLongestWord(str) { | |
var string = str.split(" "); // so we split the string every time there is a space | |
var longest = 0; // we establish that when we begin, the longest word is 0 characters long | |
var word = null; // we establish that the current word is an empty variable atm | |
for (var i = 0; i <= string.length - 1; i++) { //we establish that i = 0, and that i is less than or equal to the length of the compared string, if so add one to i | |
if (longest < string[i].length) { // if the compared word is less than the compared word in the array... | |
longest = string[i].length; // then the compared word is now the longest word | |
word = string[i]; // word is equal to the string being compared atm | |
} | |
} | |
return word.length; //output word.length from the function lol | |
} | |
findLongestWord("What if we try a super-long word such as otorhinolaryngology"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment