Created
December 3, 2015 06:21
-
-
Save anonymous/60078353df6edf62f18a 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
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
// 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"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.