Skip to content

Instantly share code, notes, and snippets.

@waltzaround
Last active September 29, 2015 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waltzaround/b83c0b276d4fa2bdf0b6 to your computer and use it in GitHub Desktop.
Save waltzaround/b83c0b276d4fa2bdf0b6 to your computer and use it in GitHub Desktop.
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