# Find the Longest Word in a String...How could I start it?

**URL:** <https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016>\
**Category:** General\
**Created:** [February 16, 2017, 2:51pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016 "2017-02-16T14:51:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![CoderInFreeCodeCamp](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/coderinfreecodecamp/32/90950_2.png) [@CoderInFreeCodeCamp](https://forum.freecodecamp.org/u/CoderInFreeCodeCamp)\
**Post date:** [February 16, 2017, 2:51pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/1 "2017-02-16T14:51:29Z")

</div>

Hi,

Could somebody give me a hint how to start this and why to start it that way? I have checked the string.length and string.prototype.split() in javascript pages, but I don’t really understand.

I guess I have to split it into an array, and then check, which of the words/_elements_ in the array is longest, but how…?

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [February 16, 2017, 3:17pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/2 "2017-02-16T15:17:32Z")

</div>

> [@CoderInFreeCodeCamp](#):
>
> I have checked the string.length and string.prototype.split() in javascript pages, but I don’t really understand.

Which part don’t you understand?

> [@CoderInFreeCodeCamp](#):
>
> I guess I have to split it into an array, and then check, which of the words/_elements_ in the array is longest…

This is the perfect way to start any programming challenge. Put it into words before you write a single line of code. You’re definitely on the right track. Imagine you had an array of numbers. How would you write a function that goes through each number and returns the largest?

---

<div class="post-metadata">

**Author:** ![janschreiber](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/janschreiber/32/160937_2.png) [@janschreiber](https://forum.freecodecamp.org/u/janschreiber)\
**Post date:** [February 16, 2017, 3:53pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/3 "2017-02-16T15:53:03Z")

</div>

> [@PortableStick](#):
>
> How would you write a function that goes through each number and returns the largest?

Spoilers below. Try to figure it out on your own first.

There are several ways to do this. One way would be to set a variable `largest` to 0, then iterate through your array of numbers. If the current number is greater than `largest`, set `largest` to the current number.  
Or [google javascript find largest number array](https://www.google.de/search?q=javascript+find+largest+number+in+array). This will point you to a way to do it with `Math.max`.

---

<div class="post-metadata">

**Author:** ![namrox](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/namrox/32/107378_2.png) [@namrox](https://forum.freecodecamp.org/u/namrox)\
**Post date:** [February 16, 2017, 4:05pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/4 "2017-02-16T16:05:55Z")

</div>

You are on the right way. First create an algorithm or couple sentences before doing coding part.

So use split() method for couple times or watch some videos. Why people use split(), why they shouldnt etc. After that check what is string.length property. You can use console.log() to exercise both. Than check explanation on FCC for that challenge. Remember if and for and while and all those concepts. After that you still dont undersand check FCC wiki on github. They are explaining solution.

Btw there is no one solution. Remember that.

---

<div class="post-metadata">

**Author:** ![CoderInFreeCodeCamp](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/coderinfreecodecamp/32/90950_2.png) [@CoderInFreeCodeCamp](https://forum.freecodecamp.org/u/CoderInFreeCodeCamp)\
**Post date:** [February 16, 2017, 4:06pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/5 "2017-02-16T16:06:38Z")

</div>

> [@PortableStick](#):
>
> Which part don’t you understand?

I am not sure how to split it into an array. I try to find out how I can use the examples from string.prototype.split in this case.

---

<div class="post-metadata">

**Author:** ![RougeRedWired](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rougeredwired/32/163053_2.png) [@RougeRedWired](https://forum.freecodecamp.org/u/RougeRedWired)\
**Post date:** [February 16, 2017, 4:07pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/6 "2017-02-16T16:07:27Z")

</div>

Hi,

A hint would be to looking at the last 10 challenges you have had in the Javascrypt object-oriented functional programming section of the course.

Cheers!

---

<div class="post-metadata">

**Author:** ![brendan-donegan](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/brendan-donegan/32/163437_2.png) [@brendan-donegan](https://forum.freecodecamp.org/u/brendan-donegan)\
**Post date:** [February 16, 2017, 4:52pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/7 "2017-02-16T16:52:17Z")

</div>

You’re on the right track with what you said in your post, so where exactly are you stuck? Is it in writing the code itself or do you not know how to do a particular thing.

It’s a good idea to post some code and indicate what problem you’re having

---

<div class="post-metadata">

**Author:** ![Cowwy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cowwy/32/158666_2.png) [@Cowwy](https://forum.freecodecamp.org/u/Cowwy)\
**Post date:** [February 16, 2017, 7:30pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/8 "2017-02-16T19:30:36Z")

</div>

Is kinda weird at first. Because you can’t understand what you don’t really use.

After some time, you will eventually understand them and you will rethink these questions in computer terms. At the moment. Think of how a human would do this.

You have a full length of string. Or a sentence and you are ask to point out the longest word in that sentence.

Analyze carefully the process you took and write that out in pseudo code. As in sentence by sentence instruction how to solve this problem.

Then you will research how to accomplish each task. For example.

Step 1: check each word and count how many alphabets there are.

Step 2: write them down and check which one is the longest.

Stuff like that.

---

<div class="post-metadata">

**Author:** ![CoderInFreeCodeCamp](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/coderinfreecodecamp/32/90950_2.png) [@CoderInFreeCodeCamp](https://forum.freecodecamp.org/u/CoderInFreeCodeCamp)\
**Post date:** [June 6, 2017, 12:52pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/9 "2017-06-06T12:52:03Z")

</div>

Feels that it’s really hard for me to figure out the answer for this.

I just get this far and it’s still wrong:

```auto
function findLongestWord(str) {
  var splits = str.split(' ');
  console.log(splits);
  return str.length;
}
var str = "The quick brown fox jumped over the lazy dog";
findLongestWord("The quick brown fox jumped over the lazy dog");

```

---

<div class="post-metadata">

**Author:** ![janschreiber](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/janschreiber/32/160937_2.png) [@janschreiber](https://forum.freecodecamp.org/u/janschreiber)\
**Post date:** [June 6, 2017, 1:00pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/10 "2017-06-06T13:00:28Z")

</div>

You are returning the length of the original string here.  
You have to iterate through your splits array.

---

<div class="post-metadata">

**Author:** ![CoderInFreeCodeCamp](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/coderinfreecodecamp/32/90950_2.png) [@CoderInFreeCodeCamp](https://forum.freecodecamp.org/u/CoderInFreeCodeCamp)\
**Post date:** [June 6, 2017, 1:45pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/11 "2017-06-06T13:45:05Z")

</div>

I don’t get anywhere.

```auto
function splitString(stringToSplit, separator) {
  var arrayofstrings = stringToSplit.split(separator);
 
}
function findLongestWord(str) {
  return str.length;
}

var str = "The quick brown fox jumped over the lazy dog";
var space = ' ';
splitString(str, space);

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

```

---

<div class="post-metadata">

**Author:** ![janschreiber](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/janschreiber/32/160937_2.png) [@janschreiber](https://forum.freecodecamp.org/u/janschreiber)\
**Post date:** [June 6, 2017, 1:53pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/12 "2017-06-06T13:53:11Z")

</div>

> [@CoderInFreeCodeCamp](#):
>
> `return str.length;`

Again, you’re returning the length of the string “The quick brown fox jumped over the lazy dog”. That can’t work.

---

<div class="post-metadata">

**Author:** ![CoderInFreeCodeCamp](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/coderinfreecodecamp/32/90950_2.png) [@CoderInFreeCodeCamp](https://forum.freecodecamp.org/u/CoderInFreeCodeCamp)\
**Post date:** [June 6, 2017, 1:59pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/13 "2017-06-06T13:59:48Z")

</div>

> [@namrox](#):
>
> split() method

I don’t really understand this task…What should I split? Should I split this “The quick brown fox jumped over the lazy dog” into an array of words?And then calculate which one is the longest? I have problems understanding the meaning of this task.

---

<div class="post-metadata">

**Author:** ![janschreiber](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/janschreiber/32/160937_2.png) [@janschreiber](https://forum.freecodecamp.org/u/janschreiber)\
**Post date:** [June 6, 2017, 2:10pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/14 "2017-06-06T14:10:16Z")

</div>

> [@CoderInFreeCodeCamp](#):
>
> I don’t really understand this task…What should I split? Should I split this “The quick brown fox jumped over the lazy dog” into an array of words?And then calculate which one is the longest?

Yes, exactly! I think you got it now.

---

<div class="post-metadata">

**Author:** ![CoderInFreeCodeCamp](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/coderinfreecodecamp/32/90950_2.png) [@CoderInFreeCodeCamp](https://forum.freecodecamp.org/u/CoderInFreeCodeCamp)\
**Post date:** [June 6, 2017, 2:41pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/15 "2017-06-06T14:41:35Z")

</div>

This still goes wrong.

```auto
var myString = "The quick brown fox jumped over the lazy dog";
var space = " ";

function splitString(stringToSplit, separator) {
  var arrayOfStrings = stringToSplit.split(separator);
}
splitString(myString, space);

function findLongestWord(str) {
  return str.length;
}

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

```

I tried also other ways to turn it into an array first.

---

<div class="post-metadata">

**Author:** ![CoderInFreeCodeCamp](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/coderinfreecodecamp/32/90950_2.png) [@CoderInFreeCodeCamp](https://forum.freecodecamp.org/u/CoderInFreeCodeCamp)\
**Post date:** [June 6, 2017, 2:44pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/16 "2017-06-06T14:44:23Z")

</div>

I tried also this (found it on [w3resource.com](http://w3resource.com))

```auto
string_to_array = function (str) {
     return str.trim().split(" ");
};
console.log(string_to_array("The quick brown fox jumped over the lazy dog"));

function findLongestWord(str) {
  return str.length;
}

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

```

---

<div class="post-metadata">

**Author:** ![janschreiber](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/janschreiber/32/160937_2.png) [@janschreiber](https://forum.freecodecamp.org/u/janschreiber)\
**Post date:** [June 6, 2017, 10:16pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/17 "2017-06-06T22:16:42Z")

</div>

The problem is in `findLongestWord`. For the umpteenth time, the assignment is to find the longest word, not the length of the original string.  
This might help to get you unstuck:

> <https://stackoverflow.com/questions/19987389/find-the-longest-word-string-in-an-array>

HTH

---

<div class="post-metadata">

**Author:** ![F4ndomGh0st](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/f4ndomgh0st/32/52271_2.png) [@F4ndomGh0st](https://forum.freecodecamp.org/u/F4ndomGh0st)\
**Post date:** [June 27, 2017, 5:18pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/18 "2017-06-27T17:18:27Z")

</div>

I just got through this and think I used a rather odd method. I’ll talk through it if the OP is still having trouble it might help.

\*\* This is how I figured it out, as always there are multiple ways and usually a better one, but understanding is the most important part \*\*

- First part you already got, take the string and split it into an array. This way you can test each word instead of the whole thing.  
`"This is a string" becomes an array containing [This, is, a, string]`
- Now to test the length of each of those elements, you can use a switch, if statements or loops. Loops seem the easiest and simplest for me.
- Since this task asks only for the length of the longest word, the actual words aren’t really necessary. I decided to iterate through the array and count the length of each word adding it to a new array.
- With a new numbered array, I sort it using the method taught in an earlier challenge which gives an ordered array  
`[1, 2, 4, 5] for example, based on the array above`
- Finally I simply return the final element from this array, knowing it will be the largest number.

Whew, probably a lot more going on than necessary, but I did it myself and like the way I came up with a solution. Also, twenty minutes ago I was getting pretty annoyed by it, so just find that one thing holding you back and you’re good.

---

<div class="post-metadata">

**Author:** ![tmpbit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tmpbit/32/48843_2.png) [@tmpbit](https://forum.freecodecamp.org/u/tmpbit)\
**Post date:** [June 27, 2017, 7:05pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/19 "2017-06-27T19:05:59Z")

</div>

CoderInFreeCodeCamp you have to split a sentence (one long stream of characters/letters, the sentence is a string) into words (separate strings). Afterwards you need to count each word’s letters iterating over an array (words, not the sentence). you can for example keep the result in the separate variable. There’s a big chance you’ll have to change the variable (result) if you find a longer word. At the end you have to provide the number which represents the number of characters in the longest string/word.  
Don’t forget to let us know whether you’ve solved it or not.

---

<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, 2:47pm UTC](https://forum.freecodecamp.org/t/find-the-longest-word-in-a-string-how-could-i-start-it/88016/20 "2021-01-16T14:47:39Z")

</div>


