Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdesire/be8492252e5b30e454ad989d62434f16 to your computer and use it in GitHub Desktop.
Save tdesire/be8492252e5b30e454ad989d62434f16 to your computer and use it in GitHub Desktop.
\\(num) -- refers to the line number of the original code.\\
(6) Accepts text (a body of text) as an argument.
(7) Passes text to the getTokens function as an argument, which removes all punctuation marks (falsy characters)
from text and adds the filtered text to a new array.
This new array is then sorted (alphabetically by default) and set to the variable words.
(8) Sets variable wordFrequencies to an empty object. Each key in this object will correspond to a specific word in words, and each
key value represents the number of times that word has occured in the text.
(9-15) Iterates through the words array; if the word in words is not a key in wordFrequencies, then it is a new word, and is added
to wordFrequencies with a key value set to 1 (since it has occured once). If word in words is in wordFrequencies, then add 1
to its frequency count (represented by its key value).
(16) Creates variable currentMaxKey (which represents the most frequently used word), and by default, sets it to the very first word in
wordFrequencies.
(17) Creates variable currentMaXCount (which represents the frequency count of the most frequently used word), and by default, sets it to
the key value (frequency count) of the very first word in wordFrequencies.
(19-24) Iterates through wordFrequencies and checks to see if the key value (frequency count) of the current key (word) is greater than
currentMaxCount. If so, currentMaxKey is replaced with the new most frequented word, and currentMaxCount with its frequency count.
(25) Returns the key (word) with the highest frequency count.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment