Skip to content

Instantly share code, notes, and snippets.

@AndriiBozh
Created January 7, 2018 18:32
Show Gist options
  • Save AndriiBozh/5800ffda2990920e341d89bc0c9ad292 to your computer and use it in GitHub Desktop.
Save AndriiBozh/5800ffda2990920e341d89bc0c9ad292 to your computer and use it in GitHub Desktop.
Find the Longest Word in a String (freeCodeCamp challenge)
function findLongestWord(str) {
var array = str.split(' '); // split a string object into an array of strings (separator is ' ')
var longestWord = 0;
for (var i = 0; i < array.length; i++){
if (array[i].length > longestWord) { // check if length (its type=number) of each string in the array is bigger than the next one
longestWord = array[i].length; // [if true] assign bigger number ([i].length) to 'longestWord' variable
}
}
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