Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The mostFrequentWord function first calls on the getTokens function, which essentially cleans up the body of text to
- create and return an all lower-case, alphabetized array to the mostFrequentWord function, stored as a new variable called
- words.
- We then create a new empty object called wordFrequencies where we will store each new word as a key and associate the
- number of times it shows up in the text as the property value.
- 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
- item currently being looked at in the array exists as a key in the wordFrequencies object, we add one (1) to the count
- for how many times that number has appeared. Otherwise, if the item does not exist as a key in the object, we create a
- new key in the object with the item (whatever the new word is) and set its property (count) to 1. The for loop will
- continue to do this until it has gone through all of the items (words) in the array.
- We then begin the process of looking for the word that shows up the most times. First we set the currentMaxKey to the
- first key in the object, and the currentMaxCount to the count/number associated with that key. Nesxt we loop through all
- of the keys in the object, and if the value associated with a key is larger than the currentMaxCount, currentMaxKey and
- currentMaxCount are re-assigned to the key and value that is currently being looped over. After all of the keys in
- 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