java-script:reduce:find-longest-word-from-a-string
This is an old revision of the document!
Find longest word from a string
- longest-word-string.js
function findLongestWord(string) { if (typeof string !== 'string' || string.trim().length === 0) { return ''; } return string.trim().split(/\s+/).reduce((maxWord, word) => { return word.length > maxWord.length ? word : maxWord; }, ''); } let longestWord = findLongestWord(" abc def ghiiii axiiii "); console.log(longestWord); // Output: "ghiiii"
java-script/reduce/find-longest-word-from-a-string.1691431016.txt.gz · Last modified: 2023/08/07 20:56 by odefta