E
E
EVGENIY ARAPOV2020-02-19 22:38:35
JavaScript
EVGENIY ARAPOV, 2020-02-19 22:38:35

Why Cannot read property 'length' of undefined error?

an error in line 1.. it is impossible so whether to take the length of the line..or am I accessing the array element and do not get the line?

function findLongest(str) {
  
  var spl = str.split(" ");
  var longest = 0
  
  for (var i = 0; i <= spl.length; i++) {
    if (spl[i].length > longest) {    //(1)
      longest = spl[i].length;
    }
    }
    return longest;
}
console.log(findLongest("The quick white fox jumped around the massive dog"), 7);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question