Tell us what’s happening:
I have been trying to research why does the result shows the error that array[i]
is undefined, but haven’t found any answers. I think that my code is not working because of something about the scope of the variables, but I cannot seem to find the solution. Would anyone be kind enough to point me in the right direction?
Thanks!
Your code so far
function findLongestWord(str) {
var array = str.split(" ");
var longest = array[0];
for (var i = 1; i <= array.length; i++) {
if (array[i].length >= longest.length) {
longest = array[i];
}
}
return longest.lenght;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0
.
Link to the challenge:
https://www.freecodecamp.org/challenges/find-the-longest-word-in-a-string