Finding the longest word in the array

I don’t get it. I just don’t get it. I’m trying to not look for the answer, because I want to solve this the hard way. Could anyone give me some little tips, without giving too much away, as to where i’m going wrong?

I have to find the longest word in the array.

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

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

Splitting the string into an array works fine. Assigning longestWord to splitStringArr[0] works fine too. I’ve tested those individual parts and I can play around with them. It’s all fine there. The problem seems to be here:

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

The error it is giving me is "cannot read property “length” of undefined. Okay. That must mean that either splitStringArr or longestWord or splitStringArr[i+1] is undefined. But why? What’s the case here?

splitStringArr.length should be 9;
longestWord.length should be 3 in the first loop;
splitStringArr[i+1].length should be 5 in the first loop;

Why does one of them come back as undefined? Thank you for reading.

1 Like

The reason you’re getting undefined is because the way you have it, you’re running your loop ONE too many times. See if that info helps.

1 Like

I misspoke. You’re not running it too many times, but on the last run, you’re asking for one more element than the array actually has. So if it has 9 elements, you’re asking for the 10th element on the last run.

1 Like

ah! of course!

how can it run [i+1] = [8+1] = [9] when it doesnt exist! the last one in the array being [8].

2 Likes

You got it! Little things like that can get SUPER frustrating. Just remember that everyone goes through it. It’s totally normal when learning this stuff. Keep at it and don’t give up!! Good work.

Split the array, loop threw it. Have a, for instance var longest = "" variable to save the longest word.

So array = array.split("")

Your loop for (var i = 0; i < array.length; i++)

Now if array[i].length > longest.length,
longest = array[i]

At the end you will return longest;


Now if you wanted to return the length of the longest word then var longest = 0

Now if array[i].length > longest,
longest = array[i].length

.length will return the longest one’s length.

At the end you will return longest;

Would you say that the way I did it was inefficient?

Here’s my code after fixing it:

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

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

It works, but I guess I can imagine that there’s a better way to do it. Almost always is with any code. I guess my question is - Is it poor quality?

You could always shorten your variables to remove context. Make it easier to read, and add documentation elsewhere to understand it.

I don’t know if it’s against the rules to use other methods either, but here is what I did.

Summary
function findLongestWord(str) {
  var result = str.split(" ");
  var count = 0;
  result.forEach(function(x,y,z){
    if( x.length > count ){ count = x.length; }
  });

  return count;
}

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

This returns the longest word and its length.

You can run this code here —> https://repl.it/JpX0/6


function findLongestWord(str) {
var words =  str.split(' ');
var longest = "".split("");
for (var i = 0; i < words.length; i++) { 
  if (words[i].length > longest.length) {
    longest = words[i];
  }
}
return longest+ ", " +longest.length;
}
findLongestWord("The quick brown fox jumped over the lazy dog");

function allLongestStrings(inputArray){

const longestString = [];

let longestWord = 0;

inputArray.forEach((word) => {

  longestWord = longestWord < word.length ? word.length : longestWord;

});

inputArray.forEach((word) => {

  if (word.length === longestWord) {

    longestString.push(word);

  }

});

return longestString;

}