Skip to content

Instantly share code, notes, and snippets.

Created December 3, 2015 22:16
Show Gist options
  • Save anonymous/62767f59bc88b53e4106 to your computer and use it in GitHub Desktop.
Save anonymous/62767f59bc88b53e4106 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/robertsheacole 's solution for Bonfire: Find the Longest Word in a String
// Bonfire: Find the Longest Word in a String
// Author: @robertsheacole
// Challenge: http://www.freecodecamp.com/challenges/bonfire-find-the-longest-word-in-a-string
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function findLongestWord(str) {
//split string into array
var stringToArray = str.split(' ');
var longestWord = 0;
//loop through the array and get the length of first item
for (var i = 0; i < stringToArray.length; i++) {
//compare length of first item to next item in array
if (longestWord < stringToArray[i].length) {
//if next item is longer set it as new longest
longestWord = stringToArray[i].length;
}
}
//return length of longest word
return longestWord;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment