I am working on the “find the longest word in the string” challenge. I’m not done but I am getting an error I don’t understand:
Type error: Cannot read property ‘length’ of undefined
Here is my code:
function findLongestWord(str) {
var t = str.split(' ');
var i =t.length;
for (i; i>0; i--){
var len = t[i].length;
}
return t;
}
If I change variable ‘i’ on this line var len = t[i].length;
to a number it will give me the length of that element. It doesn’t work with the variable. I am trying to get the length of each element in one array ‘t’ and store it in a new array ‘len’.
Thanks for your help.