re: Find longest word in a given string VIEW POST


Same thing, but using a reducer instead of sorting.

const getLongestWordOf = (sentence = '') => {
  return sentence
    .split(' ')
    .reduce((longest, current) => {
      return current.length > longest.length ? current : longest;


getLongestWordOf('I am just another solution to the same problem');

EDIT: This approach seems to be faster than the sorting one!

code of conduct - report abuse