Skip to content

Instantly share code, notes, and snippets.

@AbeEstrada
Created September 17, 2025 00:18
Show Gist options
  • Save AbeEstrada/ef00e0a9c6ca1871267640c2315035a1 to your computer and use it in GitHub Desktop.
Save AbeEstrada/ef00e0a9c6ca1871267640c2315035a1 to your computer and use it in GitHub Desktop.
Exercise: find the longest word in a sentence
const longestWord = (str: string): void => {
if (str?.length === 0) return;
const words: string[] = str.split(" ");
const longest: string = words.reduce(
(a, b) => (b.length > a.length ? b : a),
"",
);
console.log(longest.length, longest);
};
longestWord("The quick brown fox jumped over the lazy dog");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment