Algorithm 101: 6 Ways to Find the Longest Word in a Sentence
Tags: algorithms, javascript, string, word
Published: 2020-03-09 15:00:44

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.

```javascript

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()

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

* forEach(), if...statement and .split()

```javascript
      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()

```javascript
      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()

```javascript
      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()

```javascript
      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()

```javascript
      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.

> Up Next: [Algorithm 101: 9 Ways to Search and Replace a Word](https://dev.to/ebereplenty/algorithm-101-9-ways-to-search-and-replace-a-word-24jk)

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

**[Twitter](https://twitter.com/eberetwit) | [LinkedIn](https://www.linkedin.com/in/samson-ebere-njoku-profile/) | [Github](https://github.com/EBEREGIT)**

Thank You For Your Time.