Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. The mostFrequentWord function first calls on the getTokens function, which essentially cleans up the body of text to
  2. create and return an all lower-case, alphabetized array to the mostFrequentWord function, stored as a new variable called
  3. words.
  4.  
  5. We then create a new empty object called wordFrequencies where we will store each new word as a key and associate the
  6. number of times it shows up in the text as the property value.
  7.  
  8. The next process is a for loop that looks at each item in the array. As it does so, one of two things can occur. If the
  9. item currently being looked at in the array exists as a key in the wordFrequencies object, we add one (1) to the count
  10. for how many times that number has appeared. Otherwise, if the item does not exist as a key in the object, we create a
  11. new key in the object with the item (whatever the new word is) and set its property (count) to 1. The for loop will
  12. continue to do this until it has gone through all of the items (words) in the array.
  13.  
  14. We then begin the process of looking for the word that shows up the most times. First we set the currentMaxKey to the
  15. first key in the object, and the currentMaxCount to the count/number associated with that key. Nesxt we loop through all
  16. of the keys in the object, and if the value associated with a key is larger than the currentMaxCount, currentMaxKey and
  17. currentMaxCount are re-assigned to the key and value that is currently being looped over. After all of the keys in
  18. wordFrequencies have been looped over we return the currentMaxKey, which is the word that appears the most number of times.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement