Three Ways to Find the Longest Word in a String in JavaScript

Camille Kimberly | unsplash.com

This article is based on Free Code Camp Basic Algorithm Scripting “Find the Longest Word in a String”.

In this algorithm, we want to look at each individual word and count how many letters are in each. Then, compare the counts to determine which word has the most characters and return the length of the longest word.

In this article, I’m going to explain three approaches. First with a FOR loop, second using the sort() method, and third using the reduce() method.


Algorithm Challenge

Return the length of the longest word in the provided sentence.
Your response should be a number.

Provided test cases

  • findLongestWord(“The quick brown fox jumped over the lazy dog”) should return a number
  • findLongestWord(“The quick brown fox jumped over the lazy dog”) should return 6
  • findLongestWord(“May the force be with you”) should return 5
  • findLongestWord(“Google do a barrel roll”) should return 6
  • findLongestWord(“What is the average airspeed velocity of an unladen swallow”) should return 8
  • findLongestWord(“What if we try a super-long word such as otorhinolaryngology”) should return 19

1. Find the Longest Word With a FOR Loop

For this solution, we will use the String.prototype.split() method

  • The split() method splits a String object into an array of strings by separating the string into sub strings.

We will need to add an empty space between the parenthesis of the split() method,

var strSplit = “The quick brown fox jumped over the lazy dog”.split(‘ ‘);

which will output an array of separated words:

var strSplit = [“The”, “quick”, “brown”, “fox”, “jumped”, “over”, “the”, “lazy”, “dog”];

If you don’t add the space in the parenthesis, you will have this output:

var strSplit = 
[“T”, “h”, “e”, “ “, “q”, “u”, “i”, “c”, “k”, “ “, “b”, “r”, “o”, “w”, “n”, “ “, “f”, “o”, “x”, “ “, “j”, “u”, “m”, “p”, “e”, “d”, “ “, “o”, “v”, “e”, “r”, “ “, “t”, “h”, “e”, “ “, “l”, “a”, “z”, “y”, “ “, “d”, “o”, “g”];

Without comments:


2. Find the Longest Word With the sort() Method

For this solution, we will use the Array.prototype.sort() method to sort the array by some ordering criterion and then return the length of the first element of this array.

  • The sort() method sorts the elements of an array in place and returns the array.

In our case, if we just sort the array

var sortArray = [“The”, “quick”, “brown”, “fox”, “jumped”, “over”, “the”, “lazy”, “dog”].sort();

we will have this output:

var sortArray = [“The”, “brown”, “dog”, “fox”, “jumped”, “lazy”, “over”, “quick”, “the”];

In Unicode, numbers come before upper case letters, which come before lower case letters.

We need to sort the elements by some ordering criterion,

[].sort(function(firstElement, secondElement) { 
return secondElement.length — firstElement.length;
})

where the length of the second element is compared to the length of the first element in the array.

Without comments:


3. Find the Longest Word With the reduce() Method

For this solution, we will use the Array.prototype.reduce().

  • The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.

reduce() executes a callback function once for each element present in the array.

You can provide an initial value as the second argument to reduce, here we will add an empty string “”.

[].reduce(function(previousValue, currentValue) {...}, “”);

Without comments:


“How to Solve FCC Algorithms” is a series of articles on the Free Code Camp Algorithm Challenges where I try to propose several solutions and explain step by step what happens behind the hood.

If you have your own solution or any suggestions, share them below in the comments.

Or you can follow me on Medium, Twitter, Github and LinkedIn, right after you click the green heart below ;-)

‪#‎StayCurious‬, ‪#‎KeepOnHacking‬ & ‪#‎MakeItHappen‬!