Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joePichardo/5ddc94ecfa1a230069f2 to your computer and use it in GitHub Desktop.
Save joePichardo/5ddc94ecfa1a230069f2 to your computer and use it in GitHub Desktop.
Find the Longest Word in a String
// Bonfire: Find the Longest Word in a String
// Author: @joepichardo
// 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 str into array for each space in the sentence
//console.log helps us validate and visualize this.
var strArray = str.split(" ");
console.log(strArray);
var stringLength = 0;
for(var i = 0; i < strArray.length; i++){
if(stringLength < strArray[i].length){
stringLength = strArray[i].length;
}
}
return stringLength;
}
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