Tell us what’s happening:
“TypeError: Cannot read property ‘length’ of undefined”
I can’t figure out what I’ve done wrong. It seems like this code should be the correct way to do it, but for the life of me I dont’ see what I’m doing wrong to have broken .length
Your code so far
function findLongestWordLength(str)
{
var splitStr = str.split(' ');
var max = 0;
for(var i = 0; i <= splitStr.length; i++)
{
if(splitStr[i].length>max)
{
max = splitStr[i].length;
}
}
return max;
}
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.88 Safari/537.36.
Same answer as JeremyLT, but with a bit more context if needed. The only thing it cannot read is splitStr[i] with i being equal to 9, since the last element of the array you defined would be at index position 8 because it starts at 0.