# Algorithm 101: 6 Ways to Find the Longest Word in a Sentence

NJOKU SAMSON EBERE ・2 min read

This episode of Algorithm 101 features finding the longest word. In how many ways can you do this?

We will be looking at 6 ways to Find the Longest Word in a Sentence or group of words.


longestWord("Njoku Samson Ebere"); // Samson

longestWord("Find the longest word"); // longest



### Prerequisite

This article assumes that you have basic understanding of javascript's string and array methods.

### Let's Find the Longest Word in a Sentence using:

• .split() and .sort()
  function longestWord(sentence) {
return sentence.split(" ").sort((word, nextWord) => nextWord.length - word.length)[0];
}

• forEach(), if...statement and .split()
      function longestWord(sentence) {
let splittedSentence = sentence.split(" ");
let maxLength = 0;
let maxWord = "";

splittedSentence.forEach(word => {
if (word.length > maxLength) {
maxLength = word.length;
maxWord = word;
}
});

return maxWord;
}

• map(), if...statement and .split()
      function longestWord(sentence) {
let splittedSentence = sentence.split(" ");
let maxLength = 0;
let maxWord = "";

splittedSentence.map(word => {
if (word.length > maxLength) {
maxLength = word.length;
maxWord = word;
}
});

return maxWord;
}

• for...loop, if...statement and .split()
      function longestWord(sentence) {
let splittedSentence = sentence.split(" ");
let maxLength = 0;
let maxWord = "";

for (let i = 0; i < splittedSentence.length; i++) {
if (splittedSentence[i].length > maxLength) {
maxLength = splittedSentence[i].length;
maxWord = splittedSentence[i];
}
}

return maxWord;
}

• for...of...loop, if...statement and .split()
      function longestWord(sentence) {
let splittedSentence = sentence.split(" ");
let maxLength = 0;
let maxWord = "";

for (word of splittedSentence) {
if (word.length > maxLength) {
maxLength = word.length;
maxWord = word;
}
}

return maxWord;
}

• for...in...loop, if...statement and .split()
      function longestWord(sentence) {
let splittedSentence = sentence.split(" ");
let maxLength = 0;
let maxWord = "";

for (word in splittedSentence) {
if (splittedSentence[word].length > maxLength) {
maxLength = splittedSentence[word].length;
maxWord = splittedSentence[word];
}
}

return maxWord;
}


### Conclusion

There are many ways to solve problems programmatically. You are only limited by your imagination. This could also be achieved using .reduce() method. Why not try it out and tell us how you did it in the comment section?

I will love to know other ways you solved yours in the comment section.

If you have questions, comments or suggestions, please drop them in the comment section.

You can also follow and message me on social media platforms.

Posted on by:

### NJOKU SAMSON EBERE

I love providing solutions to real world problems. When I am not coding, I enjoy movies, hanging out and working out.

### Discussion

Here's the reduce version for those who are wondering:

function longestWord(sentence) {
return sentence.split(" ").reduce((longestWord, currentWord) => {
return currentWord.length > longestWord.length ? currentWord : longestWord;
}, '');
}


Edited, to clarify how the code works:

1. sentence.split(" ") returns an array of words.
2. Since the previous step creates an array, we can call reduce on it.
3. Reduce takes two arguments: the "accumulator" (in this case, the longest word) and the current element (currentWord).
4. In each iteration of the callback, we return the longer of the two words.
5. Reduce initially starts with the empty string, to guarantee that there's always something longer than it.

This is awesome, Aleksandr. Works perfectly. Can you walk us through the code?

Sure, edited to clarify

Thank you for the explanation. You are good.

Hey !
I don't think you should use map like this. You use it like a forEach, and junior developer could think it's okay to use it like this while it's not :/

PamProg thank you for taking your time to read and comment. Can you explain further?

Well, your "forEach" and "map" examples are the same, so we could think that both are good to use. But the map (and I'm sure you know it) is used to make a new array, not to find something, filter something or whatever.
It's not because you can do it with map that you should use it and promote it :)

Thank you. You are awesome 😍