a guest Aug 20th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // changed noting methodology to writing notes within the code with comments. Went through each line and added a note saying what it does, and also did a variety of testing with console.log(); on things I was unsure about. Tested the functions at the end and found it working.
  3. function getTokens(rawString) {
  4.   // a named function called getTokens with a single argument (rawString)
  5.   // NB: `.filter(Boolean)` removes any falsy items from an array
  6.   return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
  7.   //any text used in the rawString argument is changed to lowercase. .toLowercase() is a built in method in JS
  8.   // .split() is a built in method that is used here to separate each word that might be separated by white space or special characters.
  9.   // .filter(Boolean) is a built in method removes falsy entries from the array and .sort() with no argument will sort words alphabetically
  10. }
  12. function mostFrequentWord(text) {
  13.   // establishes a named function called mostFrequentWord with the single argument (text)
  14.   let words = getTokens(text);
  15.   // establishes the variable called words and calls the getTokens(text) function explained earlier
  16.   let wordFrequencies = {};
  17.   // establishes an empty object called wordFrequencies
  18.   for (let i = 0; i <= words.length; i++) {
  19.     // a for loop using the standard 3 arguments: let i = 0; is the initialized counter variable, i <= words.length; is the condition by which it is told to continue; i++ dictates how the counter is to be incremented, so each time the loop happens i increases by 1.
  20.     if (words[i] in wordFrequencies) {
  21.       wordFrequencies[words[i]]++;
  22.       // if statement that if the current word (starting with words[0]) is in wordFrequencies already, then increment 1 to that number.
  23.     } else {
  24.       wordFrequencies[words[i]] = 1;
  25.       //else statement used so that if words[i] is not in wordFrequncies yet, it's count is set to 1
  26.     }
  27.   }
  28.   let currentMaxKey = Object.keys(wordFrequencies)[0];
  29.   // This line uses the built in Object.keys() to go through the wordFrequencies object and create an array of all of the keys in it. It creates a variable called currentMaxKey at [0] to start at the beginning.
  30.   console.log(currentMaxKey);
  31.   // This returns 'a' because the text is sorted in alphabetical order
  32.   let currentMaxCount = wordFrequencies[currentMaxKey];
  33.   // a variable named currentMaxCount which accesses the wordFrequencies object and calls whichever item number is aligned to the number of currentMaxKey
  35.   for (let word in wordFrequencies) {
  36.     // creates a for loop that is used to iterate through all of the words in wordFrequencies
  37.     if (wordFrequencies[word] > currentMaxCount) {
  38.       // if statement that looks for any wordFrequencies that are greater than the currentMaxCount variable established earlier
  39.       currentMaxKey = word;
  40.       // if it finds one, then currentMaxKey is changed to that new word
  41.       currentMaxCount = wordFrequencies[word];
  42.       // sets the new currentMaxCount to the number of uses of that word
  43.     }
  44.     console.log(currentMaxKey);
  45.     console.log(currentMaxCount);
  46.   }
  47.   return currentMaxKey;
  48. }
  50. console.log(mostFrequentWord('I am testing this function to make sure it is working. Testing a function like this can be rewarding because I like testing things. Testing is one of the best things you can do'));
  51. // printed => testing
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand