Find the Longest Word in a String Loop not Working?

Find the Longest Word in a String Loop not Working?
0

#1

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”);


#2

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


#3

Return keyword makes your loop only loop once


#4

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!


#5

Can you repost your latest version of your code after moving the return statement which gives the error message you last posted?


#6
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");

#7

You are so close. You have two issues to resolve. I will give hints for both.

  1. Since you know arrays use zero-based indexes, what happens if we were to reference an index of an array element which does not exist. For example, if you had an array called arr with 3 elements [‘a’, ‘b’, ‘c’], the array has a length of 3. If you refer to the arr[3] what is returned? You have that problem in your for loop.

  2. Since the Free Code Camp test cases are ran sequentially (right after each other), anything declared globally (outside the function being tested) which gets modified inside the function will start the next test case with the same value it had at the end of the previous test.

Note: Though the following test case is not used for this challenge, how would your function handle a blank string? What would it return? for example:

findLongestWord("");

This is an edge case, but something to consider when solving problems in general.


#8

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");