re: Find longest word in a given string VIEW POST

FULL DISCUSSION
 

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! jsperf.com/longestwordjs/1

 
 
code of conduct - report abuse