# Basic Algorithm Scripting - Find the longest word in a string

**URL:** <https://forum.freecodecamp.org/t/basic-algorithm-scripting-find-the-longest-word-in-a-string/395855>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [May 29, 2020, 8:05am UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-find-the-longest-word-in-a-string/395855 "2020-05-29T08:05:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ma3\_str0](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ma3_str0/32/147033_2.png) [@Ma3\_str0](https://forum.freecodecamp.org/u/Ma3_str0)\
**Post date:** [May 29, 2020, 8:05am UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-find-the-longest-word-in-a-string/395855/1 "2020-05-29T08:05:56Z")

</div>

**Tell us what’s happening:**  
Hey guys, so I more or less got the outline of my algorithm. I’m just unsure what I could implement in the loop to loop through the values of the array and get the longest string’s length. Do I have to add another loop or an if maybe? I’m just confused atm. Any advice would be helpful!

Thanks in advance for the help!

Regards  
Ma3\_str0

**Your code so far**

```js

function findLongestWordLength(str) {
let strArr = str.split();
for(let i = 0;i < strArr.length;i++){
let loopLength = strArr[i].length;
}
}

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

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0`.

**Challenge:** Find the Longest Word in a String

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/find-the-longest-word-in-a-string)**
>
> Learn to Code — For Free

---

<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:** [May 29, 2020, 8:43am UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-find-the-longest-word-in-a-string/395855/2 "2020-05-29T08:43:47Z")

</div>

> [@Ma3\_str0](#):
>
> let loopLength = strArr[i].length;

now you are putting `loopLength` each time at the length of current word

but you need to compare the various word lengths to each other to find out which is the longest

you are missing that logic

---

<div class="post-metadata">

**Author:** ![Ma3\_str0](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ma3_str0/32/147033_2.png) [@Ma3\_str0](https://forum.freecodecamp.org/u/Ma3_str0)\
**Post date:** [May 29, 2020, 9:26am UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-find-the-longest-word-in-a-string/395855/3 "2020-05-29T09:26:10Z")

</div>

Would I compare them in an if statement? Or how would I compare the lengths? Cause that’s what’s mainly confusing me.

---

<div class="post-metadata">

**Author:** ![Bam92](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bam92/32/128056_2.png) [@Bam92](https://forum.freecodecamp.org/u/Bam92)\
**Post date:** [May 29, 2020, 9:31am UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-find-the-longest-word-in-a-string/395855/4 "2020-05-29T09:31:27Z")

</div>

To solve such a challenge, you need a bit of time to think manually about the logic.

And if you can, write the pseudocode than translate it in real-language code.

There’s not at all ONE unique way to think good, no. You just need a bit of logic in your thinking. You can still refactor as many times as you want.

Here’s my suggestions for you:

1. split the param using (’ ') as separator (keep it in a var)
2. set a var as the length asked with 0 (zero) as initial value (let call it strL)
3. loop through the split var
4. if the current str length \> strL =\> strL = current str length
5. return strL

This is just one way you can do it.

---

<div class="post-metadata">

**Author:** ![Ma3\_str0](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ma3_str0/32/147033_2.png) [@Ma3\_str0](https://forum.freecodecamp.org/u/Ma3_str0)\
**Post date:** [May 29, 2020, 12:30pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-find-the-longest-word-in-a-string/395855/5 "2020-05-29T12:30:38Z")

</div>

Thanks for the response my man I’ll try those steps. When I program in general when solving problems or building a random app, I get impulsive and I improv everything along the way and tinker later. But I’ve been trying to first plan in pseudo lately and it’s been helping me slowly but surely! Thanks again for the reply!

---

<div class="post-metadata">

**Author:** ![Izge](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/izge/32/137056_2.png) [@Izge](https://forum.freecodecamp.org/u/Izge)\
**Post date:** [May 31, 2020, 4:54pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-find-the-longest-word-in-a-string/395855/6 "2020-05-31T16:54:27Z")

</div>

this is running fine in vscode but not on the fcc pen. please help

function findLongestWordLengt(str){

let bunch = str.split(" ");

let dogo = “”;

for ( let single of bunch){

```
if(single.length > dogo.length){

   dogo = single;

}

```

}

```
return str = dogo.length;

```

}

console.log(findLongestWordLengt(“The quick brown fox jumped over the lazy dog”));

---

<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:** [May 31, 2020, 8:18pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-find-the-longest-word-in-a-string/395855/7 "2020-05-31T20:18:39Z")

</div>

your function has a different name than the one used in the tests

---

<div class="post-metadata">

**Author:** ![Bam92](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bam92/32/128056_2.png) [@Bam92](https://forum.freecodecamp.org/u/Bam92)\
**Post date:** [June 2, 2020, 10:46am UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-find-the-longest-word-in-a-string/395855/8 "2020-06-02T10:46:43Z")

</div>

The function name is FCC is this `findLongestWordLength()`, yours does not have `h` at the end.

---

<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:** [June 1, 2021, 12:46pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-find-the-longest-word-in-a-string/395855/9 "2021-06-01T12:46:12Z")

</div>


