Forked from anonymous/bonfire-find-the-longest-word-in-a-string.js
Created
December 3, 2015 22:16
-
-
Save robertsheacole/f949a908d26ae5897e53 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/robertsheacole '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: @robertsheacole | |
// 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) { | |
//split string into array | |
var stringToArray = str.split(' '); | |
var longestWord = 0; | |
//loop through the array and get the length of first item | |
for (var i = 0; i < stringToArray.length; i++) { | |
//compare length of first item to next item in array | |
if (longestWord < stringToArray[i].length) { | |
//if next item is longer set it as new longest | |
longestWord = stringToArray[i].length; | |
} | |
} | |
//return length of longest word | |
return longestWord; | |
} | |
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