# \[SOLVED\] Issue with "Find the Longest Word in a String" array length in if statement

**URL:** <https://forum.freecodecamp.org/t/solved-issue-with-find-the-longest-word-in-a-string-array-length-in-if-statement/107573>\
**Category:** General\
**Created:** [April 19, 2017, 7:21pm UTC](https://forum.freecodecamp.org/t/solved-issue-with-find-the-longest-word-in-a-string-array-length-in-if-statement/107573 "2017-04-19T19:21:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dpit/32/162822_2.png) [@dpit](https://forum.freecodecamp.org/u/dpit)\
**Post date:** [April 19, 2017, 7:21pm UTC](https://forum.freecodecamp.org/t/solved-issue-with-find-the-longest-word-in-a-string-array-length-in-if-statement/107573/1 "2017-04-19T19:21:47Z")

</div>

I’m having some problems figuring out the issue with my solution. I’m getting an error related to the if statement, specifically to the part str[i].length

The error saying _TypeError cannot read property ‘length’ of undefined_

If i just do a “return str[i].length;” outside of the if statement, it outputs just fine…so I don’t get why it’s not working in the if statement.

```
function findLongestWord(str) {
  str = str.split(" ");
  var stringLength = str[0].length; // = 4
  for (var i = 1; i <= str.length; i++) { // runs 10 times
    if (str[i].length > stringLength) { 
      stringLength = str[i].length;
    }
  }
  return stringLength;
}

findLongestWord("What is the average airspeed velocity of an unladen swallow");
```

---

<div class="post-metadata">

**Author:** ![BenGitter](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bengitter/32/12192_2.png) [@BenGitter](https://forum.freecodecamp.org/u/BenGitter)\
**Post date:** [April 19, 2017, 7:26pm UTC](https://forum.freecodecamp.org/t/solved-issue-with-find-the-longest-word-in-a-string-array-length-in-if-statement/107573/2 "2017-04-19T19:26:27Z")

</div>

It is giving an error for the last “turn” of the for loop. You are looping from 1 to the string length. So, `i` gets the values 1 … 10 (In this example). But the indexes of `str` start from 0. So instead of 1 … 10, you should loop from 0 … 9.

---

<div class="post-metadata">

**Author:** ![dpit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dpit/32/162822_2.png) [@dpit](https://forum.freecodecamp.org/u/dpit)\
**Post date:** [April 19, 2017, 7:27pm UTC](https://forum.freecodecamp.org/t/solved-issue-with-find-the-longest-word-in-a-string-array-length-in-if-statement/107573/3 "2017-04-19T19:27:50Z")

</div>

That did the trick and makes complete sense, thank you!

---

<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 17, 2021, 8:52pm UTC](https://forum.freecodecamp.org/t/solved-issue-with-find-the-longest-word-in-a-string-array-length-in-if-statement/107573/4 "2021-01-17T20:52:32Z")

</div>

Closed on January 17, 2021, 8:52pm UTC


