Please help me debug

Tell us what’s happening:

Please help me debug this code. Thank you! any help would be appreciated.
it seems like var s = String(hol[i]) is causing the problem. Initially, i did not include String() to the code but it gave me a type error, cannot read property length of undefined. i’m guessing that is what is causing the error.


var hol = [];

function findLongestWordLength(str) {
hol = str.split(" ");
var m =[];
for(let i = 0; i <= hol.length; i++) {
var s = String(hol[i]);
var k = s.length ;
m.push(k);
}
var x = Math.max(...m);
return x;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36.

Challenge: Find the Longest Word in a String

Link to the challenge:

undefined is a really long word

how could that appear in your m array?

also, bring hol inside the function, you don’t need it in the global scope

1 Like

Hi @aldr1383,

Remove ‘=’ from i <= hol.length. Since the index starts from 0, it should be i<hol.length

1 Like

yes , this is it ! thank you. silly me.