Guest User

Untitled

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