Hi everyone,
I just finished the algorithm challenge “Find the longest word in a string” which requires you to find the length of the longest word and return it but I wanted to know how I could return the actual word instead of just the length. I tried returning the array index of the longest word but it will only return the first word of the string.
function findLongestWord(str) {
var splitWord = str.split(" ");
var longestWord = 0;
for (var i = 0; i < splitWord.length; i++){
if (splitWord[i].length > longestWord)
longestWord = splitWord[i];
}
return longestWord;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
This will return “The”. How can I get it to return “jumped” instead!
Thanks.