Sequence NODE_155
Easy

Find the Longest Word in a Sentence

JavaScript
Technical Specification

Given a sentence, return the longest word. If there are multiple, return any one.

Input/Output Samples
Input:"I love JavaScript"
Output:"JavaScript"
Optimal Logic Path
function longestWord(str) {
  const words = str.split(/s+/);
  let longest = "";
  for (const w of words) {
    if (w.length > longest.length) longest = w;
  }
  return longest;
}
Architectural Deep-Dive
We keep track of the longest word seen so far while iterating.