Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createWordMap(Input) {
  2.   // count the number of appearances for each word
  3.   var wordsMap = {};
  4.   Input.forEach(function(key) {
  5.     if (wordsMap.hasOwnProperty(key)) {
  6.       wordsMap[key]++;
  7.     } else {
  8.       wordsMap[key] = 1;
  9.     }});
  10.   return wordsMap;
  11. }
  12.  
  13. function sortByCount(wordsMap) {
  14.   // create the map that will go into the .cvs file
  15.   var finalWordsArray = [];
  16.  
  17.   finalWordsArray = Object.keys(wordsMap).map(function(key) {
  18.     return {
  19.       name: key,
  20.       total: wordsMap[key],
  21. //    noun: New entry goes here from the if statement which is not yet implemented,
  22. //    verb: New entry goes here from the if statement which is not yet implemented
  23. };
  24. });  
  25.   // sort by count in descending order
  26.   finalWordsArray.sort(function(a, b) {
  27.     return b.total - a.total;
  28.   });
  29.   return finalWordsArray;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement