recude() to find longest word.js

Home   »   recude() to find longest word.js

function findLongestWord (sentence) {
  const letterArray = sentence.split(" ")
  return letterArray.reduce((longestWord, currentWord) => {
    return currentWord.length > longestWord.length
      ? currentWord
      : longestWord
  })
}

console.log(findLongestWord('Gone with the wind'))
// output: 'Gone'

Leave a Reply

Your email address will not be published. Required fields are marked *