Skip to content

Instantly share code, notes, and snippets.

@Seth-Carter
Created March 7, 2022 14:30
Show Gist options
  • Save Seth-Carter/ea92d4cfe5a14abee43acbfa79354c9c to your computer and use it in GitHub Desktop.
Save Seth-Carter/ea92d4cfe5a14abee43acbfa79354c9c to your computer and use it in GitHub Desktop.
Easyship Test
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