Find The Longest Word In A Sentence: A JavaScript Solution

Question

Have the function LongestWord(sen) take the sen parameter being passed and return the longest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignor…


This content originally appeared on DEV Community and was authored by Tochi

Question

Have the function LongestWord(sen) take the sen parameter being passed and return the longest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty. Words may also contain numbers, for example "Hello world123 567".

Solution

const longestWordInArray = (sen) => {
  if (sen.length === 0) {
    return sen;
  }
  let idx = 0;
  let len = 0;
  const wordsArray = sen.replace(/[^\w ]/g, "").split(" ");
  for (let i = 0; i < wordsArray.length; i++) {
    if (wordsArray[i].length > len) {
      len = wordsArray[i].length;
      idx = i;
    }
  }

  return wordsArray[idx];
};

console.log(longestWordInArray("fun&!! time chimpanze"));

Solution Breakdown

  • The first step is to create the function called longestWordInArray. It takes one input called sen (which is just a string).
  • Next, we check if sen is empty (""). If it is we just return it. This prevents errors.
  • We set up two variables: idx, the position (index) of the longest word we find. At first, it’s 0. len, the length of the longest word so far. At first, it’s 0.
  const wordsArray = sen.replace(/[^\w ]/g, "").split(" ");

This line does two things:

  • sen.replace(/[^\w ]/g, ""): removes punctuation and special characters, so we only keep letters, numbers, and spaces. Example: "fun&!! time" becomes "fun time".
  • .split(" "): splits the sentence into an array of words (breaking at spaces). Example: "fun time chimpanze" becomes ["fun", "time", "chimpanze"].

So now we have an array of words.

  for (let i = 0; i < wordsArray.length; i++) {
    if (wordsArray[i].length > len) {
      len = wordsArray[i].length;
      idx = i;
    }
  }

This loop goes through each word in the array:

  • wordsArray[i]: the current word.
  • wordsArray[i].length: the number of characters in that word.

If the length of the current word is greater than the current value of len:

  • Update len with the new length.
  • Update idx with the position of that word. This way, after the loop ends, idx will point to the longest word.

Finally, we return the word at position idx which is the longest word we found.

Conclusion.

In short, the function above removes punctuation, splits the sentence into words, checks which word is the longest, and returns it. The time and space complexity of the solution above is O(n).

Share your solutions in the comment below.


This content originally appeared on DEV Community and was authored by Tochi


Print Share Comment Cite Upload Translate Updates
APA

Tochi | Sciencx (2025-09-15T09:06:15+00:00) Find The Longest Word In A Sentence: A JavaScript Solution. Retrieved from https://www.scien.cx/2025/09/15/find-the-longest-word-in-a-sentence-a-javascript-solution/

MLA
" » Find The Longest Word In A Sentence: A JavaScript Solution." Tochi | Sciencx - Monday September 15, 2025, https://www.scien.cx/2025/09/15/find-the-longest-word-in-a-sentence-a-javascript-solution/
HARVARD
Tochi | Sciencx Monday September 15, 2025 » Find The Longest Word In A Sentence: A JavaScript Solution., viewed ,<https://www.scien.cx/2025/09/15/find-the-longest-word-in-a-sentence-a-javascript-solution/>
VANCOUVER
Tochi | Sciencx - » Find The Longest Word In A Sentence: A JavaScript Solution. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/15/find-the-longest-word-in-a-sentence-a-javascript-solution/
CHICAGO
" » Find The Longest Word In A Sentence: A JavaScript Solution." Tochi | Sciencx - Accessed . https://www.scien.cx/2025/09/15/find-the-longest-word-in-a-sentence-a-javascript-solution/
IEEE
" » Find The Longest Word In A Sentence: A JavaScript Solution." Tochi | Sciencx [Online]. Available: https://www.scien.cx/2025/09/15/find-the-longest-word-in-a-sentence-a-javascript-solution/. [Accessed: ]
rf:citation
» Find The Longest Word In A Sentence: A JavaScript Solution | Tochi | Sciencx | https://www.scien.cx/2025/09/15/find-the-longest-word-in-a-sentence-a-javascript-solution/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.