Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. function getWordsByFrequency (text) {
  2. return text
  3. .replace('\'', '')
  4. .replace(/\W+/g, ' ')
  5. .toLowerCase()
  6. .split(' ')
  7. .reduce((acc, word) => {
  8. if (acc[word] === undefined) {
  9. acc[word] = 1
  10. } else {
  11. acc[word] += 1
  12. }
  13. return acc
  14. }, {})
  15. }
  16.  
  17. function getMaxFromObject (obj) {
  18. return Object.keys(obj)
  19. .sort((a, b) => obj[a] > obj[b] ? 1 : -1 )
  20. .map((word) => ({
  21. word, count: obj[word]
  22. }))
  23. }
  24.  
  25.  
  26. getMaxFromObject( getWordsByFrequency(foo) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement