Created
March 7, 2022 14:30
-
-
Save Seth-Carter/ea92d4cfe5a14abee43acbfa79354c9c to your computer and use it in GitHub Desktop.
Easyship Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getLongestWord = (string) => { | |
let maxLength = 0; | |
return Object.entries(string | |
.replaceAll(/\n/g, ' ') | |
.split(' ') | |
.map((word) => { | |
return word.replaceAll(/[^a-zA-Z]/g, ''); | |
}) | |
.filter(Boolean) | |
.reduce((hash, word) => { | |
maxLength = Math.max(word.length, maxLength); | |
if (word in hash) { | |
hash[word] += 1; | |
} else hash[word] = 1; | |
return hash; | |
}, {})) | |
.reduce((result, [ word, count ]) => { | |
if (word.length === maxLength) result.push({word, count}); | |
return result; | |
}, []) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment