Find the Longest Word in a String

In this freeCodeCamp algorithm we are given a sentence and we need to return the length of the longest word.

Our response should return a Number.

Test Cases

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

Psuedocode

Let's break this problem down into smaller pieces to see how we can solve the larger problem at hand. We know that we are given a sentence, and that we need to break the sentence up into the individual words. From there we know we need to iterate over the individual words and find the length. We also need to keep track of the length of the longest given word as we're iterating through the sentence, so we should probably use a counter of some sort.

// Take a sentence

    // Break up the sentence into an array of individual words
    // Initialize a counter variable
    
    // Loop through each word in the array
        // Get the length of each word
        // If the length is greater than the counter, set the counter
        
    // Return the counter

Hint: Refer to the .split() method from the Reverse a String algorithm

Solving the Algorithm

Setting up our function

function findLongestWordLength(str) {
        
};

Split the sentence into an array of individual words

Here we can use the .split() method and separate each word in the sentence at every space, using a space within the parentheses like so:

function findLongestWordLength(str) {
    var wordArr = str.split(' ');
        
    }
};

Setup a variable to count the longest length of a word

Let's initiate a variable that we can use to count the longest given word as we iterate through our array of words.

function findLongestWordLength(str) {
    var wordArr = str.split(' ');
    var count = 0;
        
    }
};

Loop through each word in the array and find the length

Here we can setup the remainder of our logic. Referring back to our psuedocode, we'll loop over each word in our wordArr and check the length of the given word. If the length of that word is larger than the count variable, which is initially set to 0, than we will set count.

function findLongestWordLength(str) {
    var wordArr = str.split(' ');
    var count = 0;
    
    for(i = 0; i < wordArr.length; i++) {
		if(wordArr[i].length > count) {
			count = wordArr[i].length;
		}
	}
    return count;
};

findLongestWordLength("The quick brown fox jumped over the lazy dog"); // Output: 6

Final Thoughts

Overall, this is a pretty straight forward freeCodeCamp algorithm that allows us to use some of the tools from the previous algorithm challenges. In this case the .split() and .length methods were particularly helpful. We also used an additional variable, in this case count, to keep track of the length of the longest given word as we looped through the wordArr.

Hopefully you found this to be a helpful walkthrough on how to find the longest word in a string!

Shoot me an email at tim@timwheeler.com with any questions and if you enjoyed this, stay tuned and subscribe below! 👇