Forked from anonymous/bonfire-find-the-longest-word-in-a-string?solution=function%20findLongestWord(str)%20%7B%0A%20%20%0A%20%20var%20myArray%20%3D%20str.split(%22%20%22)%3B%0A%20%20var%20long%20%3D%200%3B%0A%20%20%0A%20%20%2F%2Freturn%20myArray%5B0%5D%3B%0A%20%20%0A%20%2
Created
November 16, 2015 00:55
-
-
Save rjmccallumbigl/064bea50aa15cbb9817e to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/rjmccallumbigl '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: @rjmccallumbigl | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-find-the-longest-word-in-a-string?solution=function%20findLongestWord(str)%20%7B%0A%20%20%0A%20%20var%20myArray%20%3D%20str.split(%22%20%22)%3B%0A%20%20var%20long%20%3D%200%3B%0A%20%20%0A%20%20%2F%2Freturn%20myArray%5B0%5D%3B%0A%20%20%0A%20%20for%20(var%20i%20%3D%200%3B%20i%20%3C%20myArray.length%3B%20i%2B%2B)%7B%0A%20%20%20%20%0A%20%20%20%20if%20(myArray%5Bi%5D.length%20%3E%20long)%7B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20long%20%3D%20myArray%5Bi%5D.length%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%7D%0A%20%20%0A%20%20%2F%2Freturn%20str.length%3B%0A%20%20return%20long%3B%0A%7D%0A%0AfindLongestWord(%22What%20if%20we%20try%20a%20super-long%20word%20such%20as%20otorhinolaryngology%22)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function findLongestWord(str) { | |
var myArray = str.split(" "); | |
var long = 0; | |
//return myArray[0]; | |
for (var i = 0; i < myArray.length; i++){ | |
if (myArray[i].length > long){ | |
long = myArray[i].length; | |
} | |
} | |
//return str.length; | |
return long; | |
} | |
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