Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mattnwa/7ef81d6ccaa2f7cfcf57 to your computer and use it in GitHub Desktop.
Save mattnwa/7ef81d6ccaa2f7cfcf57 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/mattnwa 's solution for Bonfire: Find the Longest Word in a String
// Bonfire: Find the Longest Word in a String
// Author: @mattnwa
// 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) {
strSplit = str.split(' ');
var longest = 0;
for(var i =0; strSplit.length >i; i++){
if (strSplit[i].length > longest){
longest = strSplit[i].length;
}
}
return longest;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
@mattnwa
Copy link
Author

mattnwa commented Dec 3, 2015

I initially struggled to get through this lesson and had to search quite a bit to understand the plan behind each step. I knew this would have to use a for loop to create the array position and then to select the total length of that array. Once i noticed that a variable to "catch" the end result was an easy solution I was able to then compare the current array item length with the current longest word.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment