April 27, 2019
Javascript Challenge #7: Find Longest Word in Sentence
Javascript Challenge #7: Find Longest Word in Sentence
In this challenge, you have to create a function which should be able to find a longest word in a string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const myString = `A sentence is a textual unit consisting..................... of one or more words that are grammatically linked`; function findLongestWord(msg){ msg = msg.split(' '); msg = msg.map(word => word.replace(/\./g, '')); let longest = msg.reduce((current, previous)=>{ return current.length > previous.length ? current : previous; }, ""); return longest; } const result = findLongestWord(myString); console.log(result); |