Skip to content

Instantly share code, notes, and snippets.

@challey17
Created October 19, 2019 20:31
Show Gist options
  • Save challey17/2bcfdf75c7664ee8c348d6b10ff20ae8 to your computer and use it in GitHub Desktop.
Save challey17/2bcfdf75c7664ee8c348d6b10ff20ae8 to your computer and use it in GitHub Desktop.
On line 7, the variable “words” is created. The text is trimmed down to only lower case words with no punctuation by the getTokens function.
Line 8 wordFrequencies is declared as a function
Line 9-15 the text is checked to see if a word is repeated and if so how many times.
Line 9 iterate over the array of words // starting the count at zero
Line 10 for each element in the words array, check to see if it already exists in the wordFrequencies object
Line 11 count the number of times a word appears in the wordFrequencies object if it appears more than once
Line12-13 otherwise, it’s value is 1 //
Line 16 the word (item) used with the most frequency is placed in the first position of an array, the array is created by using the object.keys method. The key would be the word itself and the value would be the number of times counted
Line 17 the maxCount is declared as the number of times a word is used
// maxCount = number of times a word is used
// maxKey = the word itself
Line 19-24 the words are counted and the the word with the highest count, maxKey, is given the number value of how many times it was counted.
Line 19 check to see if word exists in wordFrequencies
Line 20 a Boolean function that will return true if the value of the given word is greater than the currentMaxCount
Line 21 the currentMaxKey will equal that specific word with the highest count
Line 22 the currentMaxCount will equal the value/property of that specific word
Line 25 the function mostFrequentWord, at long last, returns the most frequently used word in the text (maxKey).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment