Home

Longest Word

Resources

  1. CoderByte

Question

Have the function LongestWord(sen) take the sen parameter being passed and return the largest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty.

Examples

Input: "fun&!! time" Output: time

Input: "I love dogs" Output: love

Answer

function LongestWord(sen) { const words = sen.split(' '); let maxWord = ''; let max = 0; for (let word of words) { const filteredWord = word.replace(/[^a-zA-Z0-9]/gi, ''); if (filteredWord.length > max) { maxWord = filteredWord; max = filteredWord.length; } } // code goes here return maxWord; }