Created
January 23, 2017 17:01
-
-
Save whroman/8cc5f3528b2c9b1def36a31b38477808 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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