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: