# Finding the longest word in the array

**URL:** <https://forum.freecodecamp.org/t/finding-the-longest-word-in-the-array/154518>\
**Category:** General\
**Created:** [October 22, 2017, 5:59pm UTC](https://forum.freecodecamp.org/t/finding-the-longest-word-in-the-array/154518 "2017-10-22T17:59:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sandris](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sandris/32/76393_2.png) [@Sandris](https://forum.freecodecamp.org/u/Sandris)\
**Post date:** [October 22, 2017, 5:59pm UTC](https://forum.freecodecamp.org/t/finding-the-longest-word-in-the-array/154518/1 "2017-10-22T17:59:55Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![gjcarrow](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gjcarrow/32/160383_2.png) [@gjcarrow](https://forum.freecodecamp.org/u/gjcarrow)\
**Post date:** [October 22, 2017, 6:13pm UTC](https://forum.freecodecamp.org/t/finding-the-longest-word-in-the-array/154518/2 "2017-10-22T18:13:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gjcarrow](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gjcarrow/32/160383_2.png) [@gjcarrow](https://forum.freecodecamp.org/u/gjcarrow)\
**Post date:** [October 22, 2017, 6:17pm UTC](https://forum.freecodecamp.org/t/finding-the-longest-word-in-the-array/154518/3 "2017-10-22T18:17:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sandris](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sandris/32/76393_2.png) [@Sandris](https://forum.freecodecamp.org/u/Sandris)\
**Post date:** [October 22, 2017, 6:23pm UTC](https://forum.freecodecamp.org/t/finding-the-longest-word-in-the-array/154518/4 "2017-10-22T18:23:06Z")

</div>

ah! of course!

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

---

<div class="post-metadata">

**Author:** ![gjcarrow](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gjcarrow/32/160383_2.png) [@gjcarrow](https://forum.freecodecamp.org/u/gjcarrow)\
**Post date:** [October 22, 2017, 6:25pm UTC](https://forum.freecodecamp.org/t/finding-the-longest-word-in-the-array/154518/5 "2017-10-22T18:25:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![John-freeCodeCamp](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@John-freeCodeCamp](https://forum.freecodecamp.org/u/John-freeCodeCamp)\
**Post date:** [October 22, 2017, 7:15pm UTC](https://forum.freecodecamp.org/t/finding-the-longest-word-in-the-array/154518/6 "2017-10-22T19:15:20Z")

</div>

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;`

---

<div class="post-metadata">

**Author:** ![Sandris](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sandris/32/76393_2.png) [@Sandris](https://forum.freecodecamp.org/u/Sandris)\
**Post date:** [October 23, 2017, 5:22pm UTC](https://forum.freecodecamp.org/t/finding-the-longest-word-in-the-array/154518/7 "2017-10-23T17:22:37Z")

</div>

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

Here’s my code after fixing it:

```auto
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?

---

<div class="post-metadata">

**Author:** ![Emgo-Dev](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/emgo-dev/32/85000_2.png) [@Emgo-Dev](https://forum.freecodecamp.org/u/Emgo-Dev)\
**Post date:** [October 23, 2017, 5:30pm UTC](https://forum.freecodecamp.org/t/finding-the-longest-word-in-the-array/154518/8 "2017-10-23T17:30:54Z")

</div>

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**
>
> ```auto
> 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");
> 
> ```

---

<div class="post-metadata">

**Author:** ![John-freeCodeCamp](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@John-freeCodeCamp](https://forum.freecodecamp.org/u/John-freeCodeCamp)\
**Post date:** [October 24, 2017, 8:37pm UTC](https://forum.freecodecamp.org/t/finding-the-longest-word-in-the-array/154518/9 "2017-10-24T20:37:51Z")

</div>

This returns the longest word and its length.

You can run this code here —\> [https://repl.it/JpX0/6](https://repl.it/JpX0/6)

* * *

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

```

* * *

---

<div class="post-metadata">

**Author:** ![Onosgb](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/onosgb/32/131081_2.png) [@Onosgb](https://forum.freecodecamp.org/u/Onosgb)\
**Post date:** [January 25, 2020, 4:00am UTC](https://forum.freecodecamp.org/t/finding-the-longest-word-in-the-array/154518/10 "2020-01-25T04:00:53Z")

</div>

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;

```

}

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 1:03pm UTC](https://forum.freecodecamp.org/t/finding-the-longest-word-in-the-array/154518/11 "2021-01-16T13:03:12Z")

</div>

Closed on January 16, 2021, 1:03pm UTC


