Created
January 7, 2018 18:32
-
-
Save AndriiBozh/5800ffda2990920e341d89bc0c9ad292 to your computer and use it in GitHub Desktop.
Find the Longest Word in a String (freeCodeCamp challenge)
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
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