Find the longest word in a sentence using node js

// Function to find the longest word in a sentence
function findLongestWord(sentence) {
  const words = sentence.split(' ');
  return words.reduce((longest, current) => (current.length > longest.length ? current : longest), '');
}

// Example usage
const sentence = 'The quick brown fox jumps over the lazy dog';
const longestWord = findLongestWord(sentence);
console.log(longestWord);

 

Post your Answer