Guest User

Untitled

a guest
Apr 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. The function mostFrequentWord takes a block of text. First we declare two variables - the variable words equals getTokens with the argument text (getTokens will return its input(text) split into an array of strings) and the variable wordFrequencies equals an empty object.
  2.  
  3. Next, we set up a for loop to iterate over the array words. If an item in words is not in the wordFrequencies object, the item gets added to the object. If the item is in the object already, then we add 1 to the frequency (for example, if the word "dog" is already in the object, then it's frequency value will be 2 instead of 1).
  4.  
  5. Next, we declare two more variables. The variable currentMaxKey equals the key value for the first item in wordFrequencies (an object consisting of an array). The variable currentMaxCount equals the frequency value of the first item in wordFrequencies. So, if a word in wordFrequencies has a higher frequency value than the currentMaxCount, then that word will become the currentMaxKey and its frequency value will become the currentMaxCount. The function mostFrequentWord will then return the word that occurs most frequently (or, the value of currentMaxKey).
Add Comment
Please, Sign In to add comment