# What's wrong with this function?

**URL:** <https://forum.freecodecamp.org/t/whats-wrong-with-this-function/486813>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [November 27, 2021, 9:50am UTC](https://forum.freecodecamp.org/t/whats-wrong-with-this-function/486813 "2021-11-27T09:50:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Metry](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@Metry](https://forum.freecodecamp.org/u/Metry)\
**Post date:** [November 27, 2021, 9:50am UTC](https://forum.freecodecamp.org/t/whats-wrong-with-this-function/486813/1 "2021-11-27T09:50:25Z")

</div>

I’m trying to find the longest word in a string, and here’s my attempt.

```auto
function findLongestWordLength(str) {
  const words = str.split(" ")
  let longest = 0;

  for(let word in words){
    if(word.length > longest){
      longest = word.length;
    }
    return longest;
  }
}

```

it keeps returning 1, and I’m not sure what’s wrong with the function I wrote.

---

<div class="post-metadata">

**Author:** ![manasm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/manasm/32/311141_2.png) [@manasm](https://forum.freecodecamp.org/u/manasm)\
**Post date:** [November 27, 2021, 10:12am UTC](https://forum.freecodecamp.org/t/whats-wrong-with-this-function/486813/2 "2021-11-27T10:12:59Z")

</div>

In this syntax of the for loop, “word” is not the element in the array, but the index of the element.

So to get the actual word value, you need to write: words[word]

Of course, I would rather name it something like “key” or “index”

---

<div class="post-metadata">

**Author:** ![blessedaim](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/blessedaim/32/237889_2.png) [@blessedaim](https://forum.freecodecamp.org/u/blessedaim)\
**Post date:** [November 27, 2021, 10:17am UTC](https://forum.freecodecamp.org/t/whats-wrong-with-this-function/486813/3 "2021-11-27T10:17:45Z")

</div>

first of all, what do you want the function to return? You want it to return the length of the longest word? Or you want it to return the WORD? If you want to return the WORD, you need to have a variable for that too, such as `longestWord` and assign the word to it in a cycle.

Second, after using `split` you get an Array, not an Object, so you should use `for(... of...)` instead of `for (... in ...) `.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![manasm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/manasm/32/311141_2.png) [@manasm](https://forum.freecodecamp.org/u/manasm)\
**Post date:** [November 27, 2021, 10:20am UTC](https://forum.freecodecamp.org/t/whats-wrong-with-this-function/486813/4 "2021-11-27T10:20:35Z")

</div>

Interesting, I have never heard of “for of” and “for in” loops before this question. I have always used forEach for the same functionality, guess this is a better way.

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [November 27, 2021, 11:12am UTC](https://forum.freecodecamp.org/t/whats-wrong-with-this-function/486813/5 "2021-11-27T11:12:18Z")

</div>

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [May 28, 2022, 11:12pm UTC](https://forum.freecodecamp.org/t/whats-wrong-with-this-function/486813/6 "2022-05-28T23:12:45Z")

</div>

Closed on May 28, 2022, 11:12pm UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
