Skip to content

Instantly share code, notes, and snippets.

@whroman
Created January 23, 2017 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whroman/8cc5f3528b2c9b1def36a31b38477808 to your computer and use it in GitHub Desktop.
Save whroman/8cc5f3528b2c9b1def36a31b38477808 to your computer and use it in GitHub Desktop.
function getWordsByFrequency (text) {
return text
.replace('\'', '')
.replace(/\W+/g, ' ')
.toLowerCase()
.split(' ')
.reduce((acc, word) => {
if (acc[word] === undefined) {
acc[word] = 1
} else {
acc[word] += 1
}
return acc
}, {})
}
function getMaxFromObject (obj) {
return Object.keys(obj)
.sort((a, b) => obj[a] > obj[b] ? 1 : -1 )
.map((word) => ({
word, count: obj[word]
}))
}
getMaxFromObject( getWordsByFrequency(foo) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment