Find the Longest Word in a String Loop not Working?

I feel like I’m close here, and I don’t want the answer/solution, just a nudge in the right direction. Right now I am trying to cycle through each word in the array, find the length and set it equal to my variable ‘longestWord’. Only problem is I don’t think it is looping. I think it just looks at the first index in the array and then stops. I feel like my code is right for the loop to occur, but I could be missing something. I’ve been messing with this for a good hour trying to understand why it wouldn’t keep the loop going so if someone could help me that would be awesome!

Again, don’t want the fix/answer, just a hint at what to do to fix the issue.

function findLongestWord(str) {
var array = str.split(’ ');
var longestWord = 1;
for (var i = 0; i <= array.length; i++) {
if (array[i].length > longestWord) {
longestWord = array[i].length;
}
return longestWord;
}

}

findLongestWord(“The quick brown fox jumped over the lazy dog”);

Perhaps you are ending the function execution too early? (nudge) (nudge)

Return keyword makes your loop only loop once

Ok that makes sense, but when I move the retrun statement outside of the for loop I get this error:

“TypeError: cannot read property ‘length’ of undefined.”

I just don’t understand!

var longestWord = 1;

function findLongestWord(str) {
  var array = str.split(' ');

  for (var i = 0; i <= array.length; i++) {
    if (array[i].length > longestWord) {
      longestWord = array[i].length;
    }

  }
  return longestWord;
}

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

Thank you so much, I finally got it working haha. I knew I was close, just wasn’t quite sure where to go next!

I’ll paste my solution just in case others are having the same issue.
Working code:

function findLongestWord(str) {
   var array = str.split(' ');
   var longestWord = 1;
   for (var i = 0; i <= array.length - 1; i++) {
       if (array[i].length > longestWord) {
          longestWord = array[i].length;
       }
   }
   return longestWord;
}

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