Using a few 'modern' ES5+ constructs, we can reduce the solution to four lines of code.
Annotated version:
constlongestWord=input=>{//remove any character that isn't alphabetic (\w) or a blank space (\s) from input //and split the resulting string on blank spaces, ie: this -> 'two word(s)!' becomes -> ['two', 'words']constwords=input.replace(/[^\w\s]/g,'').split('');letmaxLen=0;//iterate over words to find the size of the longest word by comparing each length to the previous maxLenfor(constwordofwords)maxLen=Math.max(maxLen,word.length);//return all words whose length is maxLenreturnwords.filter(e=>e.length==maxLen);}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Using a few 'modern' ES5+ constructs, we can reduce the solution to four lines of code.
Annotated version: