Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getTokens(rawString) { //creates a new function named getTokens with an argument input rawString
- return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort(); //block statement returns lowercase rawstring
- //splits characters (/[ ,!.";:-]+/) filters for true & false statements and proceeds to sort the array in alphabetical order
- function mostFrequentWord(text) { //creates a new function named mostFrequentWord with text arguement input
- let words = getTokens(text); // on a local level defines the words variable as getTokens with the arguement of text
- // all the text input through mostFrequentWord undergoes the parsing of the getTokens function
- let wordFrequencies = {}; // defines a new variable wordFrequencies as an empty array?
- for (let i = 0; i <= words.length; i++) { //runs this sequence as for as long as the for arguement is valid
- if (words[i] in wordFrequencies) { //this line breaks the loop under the condition that there are no more characters in the text?
- wordFrequencies[words[i]]++; //everytime a word occurs 1 value is added?
- } else { // if the result is false then we execute the following statement
- wordFrequencies[words[i]] = 1;
- }
- }
- let currentMaxKey = Object.keys(wordFrequencies)[0]; //creates an defines an array for the frequency of words
- let currentMaxCount = wordFrequencies[currentMaxKey]; //creates our total for the max count
- //this next block takes the varibles above and decides whether a new word is larger than the current count, and if so rewrites the new word
- //as the new most frequent word
- for (let word in wordFrequencies) { //??
- if (wordFrequencies[word] > currentMaxCount) { // begins a loop to read if the new frequent word is larger than the amount from our current
- currentMaxKey = word;
- currentMaxCount = wordFrequencies[word];
- }
- }
- return currentMaxKey; //returns the results of the main function in the console
- }
Add Comment
Please, Sign In to add comment