-
-
Save anonymous/49014c849ff23c4b1656 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/twhite96 's solution for Bonfire: Find the Longest Word in a String
This file contains hidden or 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: @twhite96 | |
| // Challenge: http://www.freecodecamp.com/challenges/bonfire-find-the-longest-word-in-a-string?solution=function%20findLongestWord(str)%20%7B%0A%20%20var%20words%20%3D%20str.split(%27%20%27)%3B%0A%20%0A%20%20var%20longest%20%3D%200%3B%0A%20%20%0A%20%20for%20(var%20i%20%3D%200%3B%20i%20%3C%20words.length%3B%20i%2B%2B)%20%7B%0A%20%20%20%20if%20(words%5Bi%5D.length%20%3E%20longest)%20%7B%0A%20%20%20%20%20%20longest%20%3D%20words%5Bi%5D.length%3B%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%7D%0A%20%20%0A%20%20%20%20%20%20return%20longest%3B%0A%7D%0A%0AfindLongestWord(%22The%20quick%20brown%20fox%20jumped%20over%20the%20lazy%20dog%22)%3B%0A | |
| // Learn to Code at Free Code Camp (www.freecodecamp.com) | |
| function findLongestWord(str) { | |
| var words = str.split(' '); | |
| var longest = 0; | |
| for (var i = 0; i < words.length; i++) { | |
| if (words[i].length > longest) { | |
| longest = words[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