# Find the Longest Word in a String: Solution not working

**URL:** <https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-solution-not-working/167437>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [January 8, 2018, 5:45am UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-solution-not-working/167437 "2018-01-08T05:45:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Indie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/indie/32/82103_2.png) [@Indie](https://forum.freecodecamp.org/u/Indie)\
**Post date:** [January 8, 2018, 5:45am UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-solution-not-working/167437/1 "2018-01-08T05:45:38Z")

</div>

I was wondering if someone could take a look at the code below and tell my why it won’t pass the tests. I wrote the same code on repl.it and it works just fine, but when on FCC I fail all the test sentences except “May the force be with you”

**Your code so far**

```auto
function findLongestWord(str) {
  var array = str.split(" ");

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

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

```

**Your browser information:**

Your Browser User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7`.

**Link to the challenge:**  
[https://www.freecodecamp.org/challenges/find-the-longest-word-in-a-string](https://www.freecodecamp.org/challenges/find-the-longest-word-in-a-string)

---

<div class="post-metadata">

**Author:** ![Indie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/indie/32/82103_2.png) [@Indie](https://forum.freecodecamp.org/u/Indie)\
**Post date:** [January 8, 2018, 6:01am UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-solution-not-working/167437/2 "2018-01-08T06:01:12Z")

</div>

I figured out my issue, I have my return statement in the wrong place. Should be as follows:

function findLongestWord(str) {  
var array = str.split(" ");  
var max = array[0];

for (var i = 0; i \< array.length; i++) {  
if (max.length \< array[i].length) {  
max = array[i];  
}  
}  
return max.length;  
}

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 18, 2021, 8:32am UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-solution-not-working/167437/3 "2021-01-18T08:32:31Z")

</div>

Closed on January 18, 2021, 8:32am UTC


