# How to find the longest word in a string and return the actual word (not the length)

Hi everyone,

I just finished the algorithm challenge “Find the longest word in a string” which requires you to find the length of the longest word and return it but I wanted to know how I could return the actual word instead of just the length. I tried returning the array index of the longest word but it will only return the first word of the string.

``````function findLongestWord(str) {

var splitWord = str.split(" ");
var longestWord = 0;

for (var i = 0; i < splitWord.length; i++){
if (splitWord[i].length > longestWord)
longestWord = splitWord[i];
}

return longestWord;

}

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

``````

This will return “The”. How can I get it to return “jumped” instead!

Thanks.

You need to use triple backticks to post code to the forum.
See this post for details.

First of all, it’s awesome that you took the time to do this. Curiosity is a major component to growth as a programmer, so making little modifications to code you’ve written or read is going to take you far.

Now, I’m hesitant to give you the answer because I think you can totally get this yourself. Take the time to explain how your code works, step by step. Talk to a rubber duck - it helps. I can see the problem plainly, so I promise it’s a solvable problem. You can do this!

1 Like

To find the longest word, you have to compare lengths of two words. What you are doing is you’re comparing number to number (`splitWord[i].length > longestWord`) in first iteration.

But when this condition satisfies, you’re overwriting your `longestWord` which is initialized with a number to a string.

Then 2nd iteration onward, you’re comparing number to a string.

What you need to do is initialize your longestWord as empty string. And then compare `splitWord[i].length` and `longestWord.length` (number to number comparison)

``````function findLongestWord(str) {

var splitWord = str.split(" ");
var longestWord = '';

for (var i = 0; i < splitWord.length; i++){
if (splitWord[i].length > longestWord.length)
longestWord = splitWord[i]
}

return longestWord;

}

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

Another way to do the same is to use `.reduce` method

``````function findLongestWord(str) {
return str.split(' ')
.reduce((longest, current) => longest.length > current.length ? longest : current, 0);
}

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

for the FCC challenge where you’re expected to return length of longest word

``````function findLongestWord(str) {
return str.split(' ')
.reduce((longest, current) => longest.length > current.length ? longest : current, 0)
.length;
}

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

A small note … it is probably a good idea to strip out punctuation so that a period or comma, for example, doesn’t mess up the result.

Consider:

``````function findLongestWord(str) {
return str.split(' ')
.reduce((longest, current) => longest.length > current.length ? longest : current, 0);
}

findLongestWord("It was the season of light.");
``````

This should return `season`, but instead, it returns `light.`

A quick fix if you only care about English is:

``````function findLongestWord(str) {
return str.replace(/[^\w\s]/g, '')
.split(' ')
.reduce((longest, current) => longest.length > current.length ? longest : current, 0);
}
``````

Thank you for the breakdown of my error. I appreciate the time you took to write it all out!

1 Like

Thanks for the encouragement! I can’t wait to break my brain on the next couple challenges.

Thank you for this solution. I was looking for hours trying to to the same thing for codebyte,

Hi! Could you explain why the answer is set to an empty string rather than an empty array? Thank you!