Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The basic purpose of this algorithm is to determine what word is used most commonly in the text submitted. We create an empty object -
- wordFrequencies. We iterate through each word in the text. If the word has not been encountered yet, we create a new key-value pair for it
- in this object, and sets its value to one. If the word has been come across already and we've already created a key-value pair, we increase
- its count by one. So the mostFrequentWord function is counting how many times each word is used in the text.
- CurrentMaxKey becomes the first array item in the Object.keys array that displays all of the keys. currentMaxCount is the object key
- that currentMaxKey is set to. We start at index 0, as we're going to iterate through and compare each one.
- We iterate through the wordFrequencies object using a for-in loop. As we iterate through, we
- check to see if this key-property pair has a higher numeric value than the current one (we start with the first one in the array as the one to
- compare to). If the current key-value pair has a higher value, we then set the key to currentMaxKey and the number to currentMaxCount.
- After going through each item in the array, we will have the highest key-value numeric value assigned to these variables, thus figuring out
- which word is used most. After the loop, we return the currentMaxKey, which is the word used most frequently.
Add Comment
Please, Sign In to add comment