DEV Community

Cover image for FreeCodeCamp: Find the longest word in a string. Basic Algorithm 4
Ifeoma
Ifeoma

Posted on

FreeCodeCamp: Find the longest word in a string. Basic Algorithm 4

In the words of Prof. Jelani Nelson of Harvard University, "An algorithm is a well-defined procedure for carrying out some computational task. Typically the task is given, and the job of the algorithmist is to find such an efficient procedure, for example in terms of processing time and/or memory consumption"

A more straightforward definition in my words is that "an algorithm is a set of step-by-step instructions that indicate a process or processes and describe how a certain task should be carried out"

In this series, I will share solutions and my thought process for arriving at these solutions to the FreeCodeCamp algorithm challenges from the Javascript Algorithm and Data Structures section: Basic Algorithm

Starting with Challenge 4: Find the longest word in a string

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

function findLongestWordLength(str) {
  return str.length;
}

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

Enter fullscreen mode Exit fullscreen mode

Given the algorithm above, we will return the longest word in the given sentence.

To find the longest word in a sentence, we will iterate over the sentence and compare the length of each word to the length of the longest word found so far. To iterate through the given string "The quick brown fox jumped over the lazy dog", we have to split the words up into substrings.

Javascript has inbuilt methods provided to carry out several functions. For this string value, we will use the javascript split method. The split method takes a string value, divides it into substrings based on the separator we specify, and returns an array that can be iterated upon.

For this algorithm, we will be making use of a separator and then storing the result in a variable wordsArray, like so:

function findLongestWordLength(str) {
  let wordsArray = str.split(' ');

  // console.log (wordsArray)
  //(9) ['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
}

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

Enter fullscreen mode Exit fullscreen mode

when we log the value of wordsArray to our console, it returns an array with 9 substrings, it is these substrings that will be iterated upon.

We will store the current longest word in a variable named currentLongestWord

function findLongestWordLength(str) {
  let wordsArray = str.split(' ');
  let currentLongestWord = "";
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");
Enter fullscreen mode Exit fullscreen mode

Of course, it is stored as a string because the value, that is, the words from the argument given is a string value.

The next step will be to iterate through our given array wordsArray to iterate over an array we will be using the for...loop

function findLongestWordLength(str) {
  let wordsArray = str.split(' ');
  let currentLongestWord = "";

  for ( let i = 0; i < wordsArray.length; i++ ){

  }
}
 findLongestWordLength("The quick brown fox jumped over the lazy dog");
Enter fullscreen mode Exit fullscreen mode

n the code above, we have written a for...loop that iterates through all the items in the area stored in wordsArray and because we do not know the actual length of the elements in the array, we make use of the method length

Every time the loop runs, it iterates through each substring which becomes the current value of wordsArray[i] we can log the current value of wordsArray[i] to gain a clearer picture of how the value changes on every iteration:

function findLongestWordLength(str) {
 let wordsArray = str.split(' ');
 let currentLongestWord = "";

  for ( let i = 0; i < wordsArray.length; i++ ){
     console.log (wordsArray[i]);
//returns => 
// The
//quick
//brown
//fox
//jumped
//over
//the
//lazy
//dog
  }
}
 findLongestWordLength("The quick brown fox jumped over the lazy dog");
Enter fullscreen mode Exit fullscreen mode

This value needs to be stored in a variable so that it can be compared to the currentLongestWord so, we will be storing it in the currentWord variable. For us to compare we will make use of an if... statement so that if length of (characters) in currentWord is greater than the length of (characters) in currentLongestWord, the value of currentWord gets updated to the currentLongestWord by using the assignment symbol.

function findLongestWordLength(str) {
  let wordsArray = str.split(' ');
  let currentLongestWord = "";

  for ( let i = 0; i < wordsArray.length; i++ ){
       let currentWord = wordsArray[i];
       if (currentWord.length >   currentLongestWord.length){
           currentLongestWord = currentWord
       }
  }
return currentLongestWord.length;
}
 findLongestWordLength("The quick brown fox jumped over the lazy dog");
Enter fullscreen mode Exit fullscreen mode

The next step will be to return the length of the currentLongestWord as we have done above, we are making use of the length method because the algorithm requests that a number should be returned.

Now, log in the given argument or any other value of your choice and the number that will be returned will be the length of the longest word in the sentence.

function findLongestWordLength(str) {
let wordsArray = str.split(' ');
  let currentLongestWord = "";

  for ( let i = 0; i < wordsArray.length; i++ ){
       let currentWord = wordsArray[i];
       if (currentWord.length >   currentLongestWord.length){
           currentLongestWord = currentWord
       }
  }
return currentLongestWord.length;
}
 console.log (findLongestWordLength("The quick brown fox jumped over the lazy dog"));
//returns => 6

console.log (findLongestWordLength("Javascript is a high-level language"))
//returns => 10
Enter fullscreen mode Exit fullscreen mode

Remember, there are different ways to solve an algorithm in Javascript, solve yours in the way you understand and maybe share in the comments.

Top comments (0)