Advertisement
Guest User

Untitled

a guest
May 21st, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. When main.js is called the first thing that happens is the JS engine runs through it and creates space in memory for all the functions and global variables. It also creates an execution stack with the window object being the first execution context, a.k.a. the global execution context. The window object is created with the this variable given an initial value of global - window(global).
  2.  
  3. Then the JS engine gives the getTokens(rawString) function an execution context on the stack.
  4.  
  5. The getTokens(rawString) function converts all characters in the text to lowercase, splits out the special characters from the alphabetical characters, filters then out of the array, and then sorts the remaining strings alphabetically.
  6.  
  7. So now there is an array object in memory named getTokens, which contains all the words in the text, so the getTokens function is taken off of the execution stack.
  8.  
  9. The mostFrequentWord function goes on the stack next and assigned a variable (argument) named text. A variable named words is created and assigned the value of the getTokens array and one argument named text. The wordFrequencies variable is also created as an object with no value assigned yet.
  10.  
  11. The third thing the mostFrequentWord function does is create a for loop that parses the words array and captures the frequency with which each word occurs in the text.
  12.  
  13. Next, the JS engine creates the currentMaxKey variable in memory and assigns it the value of all the properties in the wordFrequencies array object which makes JS to render the human-readable word associated with the index number. Then the currentMaxCount variable is created and assigned the word currently containing the highest number of frequency in the text.
  14.  
  15. Finally, a for loop is created which starts by creating a variable named word and making it a property of the wordFrequencies array. Then it compares the frequency value of the current word against the value of currentMaxCount and if it evaluates to greater than, it changes the value of currentMaxKey to that word. The currentMaxCount variable is then changed to = the value of wordFrequencies[word] to end the loop, and the value of currentMaxKey (the word with the most frequency) is returned.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement