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
.
Challenge: Find the Longest Word in a String
Link to the challenge: