daily pastebin goal
19%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getTokens(rawString) {
  2.   // NB: `.filter(Boolean)` removes any falsy items from an array
  3.   return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
  4. }
  5.  
  6. function mostFrequentWord(text) {
  7.   let words = getTokens(text);
  8.   let wordFrequencies = {};
  9.   for (let i = 0; i <= words.length; i++) // setting variable of words to defined and word frequencies to a for loop. Increase variable i by 1 if words array element  is > words.length
  10.   {
  11.     if (words[i] in wordFrequencies) {
  12.       wordFrequencies[words[i]]++;
  13.       // if a word in the text of wordFrequencies array is used more than once catalog the count else..
  14.     } else {
  15.       wordFrequencies[words[i]] = 1;
  16.     }
  17.     // count a single use as 1
  18.   }
  19.   let currentMaxKey = Object.keys(wordFrequencies)[0];
  20.   //to show with an array the words that occur most frequently
  21.   let currentMaxCount = wordFrequencies[currentMaxKey];
  22.   //to show with an array the count of the currentMaxkey words
  23.  
  24.   for (let word in wordFrequencies) {
  25.     if (wordFrequencies[word] > currentMaxCount) {
  26.       currentMaxKey = word;
  27.       currentMaxCount = wordFrequencies[word];
  28.       // if a word is greater than the currentmaxcount the current max key will be the new word and the new count will be the count of the new word?
  29.     }
  30.   }
  31.   return currentMaxKey;
  32.   //this will return the array of the current max words with name and count
  33. }
RAW Paste Data
Top