Find the Longest Word in a String - why is array[i] undefined?

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?

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:

Recall that indexing starts at 0. What does that make the index for the last element in an array?

1 Like

Arrays are zero-indexed, so the largest index in an array would always be one less then the array length. Think about your for loop condition here.

Also, you have not gotten to it yet, because of the error related to the code at the top, but you have a typo in the line above.