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

Uncaugth TypeError: Cannot read length | Find longest word | SOLVED
0.0 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");