Here is my code:
function findLongestWord(str) {
//split string
var arr = str.split(' ');
var longest = 0;
//run through the array
for(var i = 0; i < arr.length; i++) {
var word = arr[i];
//I think I got it!
var wordLength = word.length;
if (wordLength > longest){
longest = wordLength;
}
}
return longest;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
It ran and it worked. I did this a week ago and did not include any comments. I was reviewing it and can’t seem to piece together how it works or why it works. I did it on the fly with a friend and now I can’t quite follow what is actually happening. Any help explaining or commenting it out would be greatly appreciated. I get lost at about the halfway point.