Skip to content

Instantly share code, notes, and snippets.

  • Save anonymous/ad82b94d9153167c383c to your computer and use it in GitHub Desktop.
Save anonymous/ad82b94d9153167c383c to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/terdia 's solution for Bonfire: Find the Longest Word in a String
// Bonfire: Find the Longest Word in a String
// Author: @terdia
// 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%20convertToArray%20%3D%20str.split(%22%20%22)%3B%0A%20%20var%20longestWord%20%3D%20%22%22%3B%0A%20%20var%20check%20%3D%20%22%22%3B%0A%20%20%0A%20%20for(var%20i%20%3D%200%3B%20i%20%3C%20convertToArray.length%3B%20i%2B%2B)%7B%0A%20%20%20%20%20%20%20%0A%20%20%20%20if(convertToArray%5Bi%5D.length%20%3E%20check.length)%7B%0A%20%20%20%20%20%20%20check%20%3D%20convertToArray%5Bi%5D%3B%20%0A%20%20%20%20%20%20%20longestWord%20%3D%20convertToArray%5Bi%5D%3B%20%0A%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%7D%0A%20%20%0A%20%20return%20longestWord.length%3B%20%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 convertToArray = str.split(" ");
var longestWord = "";
var check = "";
for(var i = 0; i < convertToArray.length; i++){
if(convertToArray[i].length > check.length){
check = convertToArray[i];
longestWord = convertToArray[i];
}
}
return longestWord.length;
}
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