How to find the longest word in a string with TypeScript

1 Answer

0 votes
function longestWord(s: string): string {
  const words: string[] = s.split(/\s+/);

  return words.reduce((max: string, w: string) =>
    w.length > max.length ? w : max
  , "");
}

console.log(longestWord("Could you recommend a good restaurant nearby?"));



/*
run:

"restaurant" 

*/

 



answered Mar 2 by avibootz
...