Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The `getTokens` function has one parameter `rawString`. When a string gets
- passed to getTokens, everything is made lower-case, and the string is split
- into an array at the characters specified by the regular expression `!, ;`
- and so forth. Then any falsy statements are removed from the array and the
- items are arranged alphabetically by `.sort()`.
- The next function is `mostFrequentWord`, which has one parameter `text`.
- Within that function, `words` is declared as a variable and is set to the
- resulting array from the `getTokens` function. The parameter `text` is shared
- by `mostFrequentWord` and `words`, so the argument string passed is the same for
- both the aforementioned functions, first to `mostFrequentWord` and then to
- `getTokens`, the result of which is stored in the variable `word`. A for loop
- then iterates over the array contained in `words`. If a particular item in `words`
- is found multiple times in the array/object(?) `wordFrequencies`, then the word
- count for that item rises once per instance. If it is not found more than once, it equals 1.
- The remainder of the code then sets this index in the `words` array to a number.
- `currentMaxKey` is then set to index 0 of the new `wordFrequencies` object,
- which in my example is `a`. Then `currentMaxCount` is set to `wordFrequencies[currentMaxKey]`,
- which is the number of times index 0 appears in the original string. In my example it appears
- once and equals 1. These two `let` declarationgs are merely setting defaults to be altered by
- the upcoming `for` loop.
- The `for` loop iterates through each of the properties(keys) in the `wordFrequencies` object,
- assigning each value from the object to the variable `word`. If the numeric value of
- `wordFrequencies[word]` is greater than `currentMaxCount` (remember the default of
- `currentMaxCount` is 0), then `currentMaxKey` is changed to the word with a higher count.
- `currentMaxCount` is also changed to the number of times that word appears in the orig.
- string.`currentMaxKey` is then returned, being the word (or the key from the
- wordFrequencies object) that appears the most in the original string.
Add Comment
Please, Sign In to add comment