Uncaugth TypeError: Cannot read length | Find longest word | SOLVED

Uncaugth TypeError: Cannot read length | Find longest word | SOLVED
0

#1

Note: I solved it! Learned that my loop was going out of bounds hence the undefined. Also made the correct code.

I think the code is correct but I can’t be sure cause I get this cannot read length property. Googling it doesn’t seem to help me.

function findLongestWord(str) {
  var arr = str.split(' ');
  var maxLength = 0;
  for (var i = 0; i < arr.length; i++) {
    if (arr[i].length > maxLength) {
      maxLength = arr[i].length;
    }
  }
  return maxLength;
}
findLongestWord("The quick brown fox jumped over the lazy dog");

#2

Basically you did write ur code correctly till maxLength = arr[i].length;
In problem you r tasked finding longest word not longest words length
So u need to declare string store whatever words length bigger than maxlength store it in this declared string
let longestWord = ‘’
longestWord = arr[i] // this gives you solution longest word in the sentence
in the above example you r returning length of the longest word