Find Longest Word Length - stuck!

I keep getting an error message saying ‘Cannot read property length of undefined’
I’m sure I’m missing something obvious here but can’t seem to figure it out. Any help is really appreciated.


function findLongestWordLength(str) {
let array = str.split('');
let longest = 0;

for (let i = 0; i <= array.length; i++) {
  if (array[i].length > longest) {
    longest = str[i].length;
  }
}
return longest;
}

console.log(findLongestWordLength("The quick brown fox jumped over the lazy dog"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36.

Challenge: Find the Longest Word in a String

Link to the challenge:

It looks like you have an ‘off by one’ error here.

Also, this is splitting by character, which isn’t what you meant, I think?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.