Skip to content

Instantly share code, notes, and snippets.

View mikedolan03's full-sized avatar

Mike Dolan mikedolan03

View GitHub Profile
@mikedolan03
mikedolan03 / stringdrills.txt
Created January 14, 2018 03:28
javascript drills
https://repl.it/@mikedolan03/Wiseperson-generator-drill
https://repl.it/@mikedolan03/shouter-drill
https://repl.it/@mikedolan03/text-normalizer-drill
https://repl.it/@mikedolan03/area-of-a-rectangle-drill
https://repl.it/@mikedolan03/temperature-conversion-drill
https://repl.it/@mikedolan03/Is-divisible-drill
https://repl.it/@mikedolan03/Traffic-lights-drill
https://repl.it/@mikedolan03/Error-alert-drill
<a href="https://repl.it/@mikedolan03/Creating-arrays-drill">create array </a>
<a href="https://repl.it/@mikedolan03/Adding-array-items-drills">adding items</a>
<a href="https://repl.it/@mikedolan03/Accessing-array-items-drill">access items</a>
<a href="https://repl.it/@mikedolan03/Array-length-and-access-drill">array length</a>
https://repl.it/@mikedolan03/Find-drill
https://repl.it/@mikedolan03/Filter-drill
https://repl.it/@mikedolan03/Sort-drill
https://repl.it/@mikedolan03/Squares-with-map-drill
https://repl.it/@mikedolan03/Array-copying-II-drill
https://repl.it/@mikedolan03/Array-copying-I-drill
https://repl.it/@mikedolan03/fizzbuzz-drill-js
https://repl.it/@mikedolan03/average-drill
https://repl.it/@mikedolan03/min-and-max-without-sort-drill
1. What is scope? Your explanation should include the idea of global vs. local scope.
I like to think of scope as the distance variables can travel within a program. If a variable has global scope is can travel anywhere and be accessed and changed in any function in the program. Global variables can also be accessed and mutated in subsequently declared javascript files. A locally scoped variable is prevented from being accessed beyond the function it was declared it. Local variables values can still be accessed from the outside by passing the value as an argument in a function -- this way the local var is safe from being changed by external functions or even external js files.
2. Why are global variables avoided?
Global variables have the allure of easy access and make it possible to write functions without passing in arguements or returning anythings because you can just change the global variable. However, this is a dangerous way to program because any new code added or external js files might unknowi
https://repl.it/@mikedolan03/Deleting-keys-drill
https://repl.it/@mikedolan03/Self-reference-drill
https://repl.it/@mikedolan03/Object-updater-drill
https://repl.it/@mikedolan03/Object-creator-drill
https://repl.it/@mikedolan03/validate-object-keys-drill
https://repl.it/@mikedolan03/find-by-id-drill
https://repl.it/@mikedolan03/Enroll-in-summer-school-drill
https://repl.it/@mikedolan03/Make-student-reports-drill
The function GetTokens takes the argument -rawstring - makes it all lowercase and then splits it any time there is a space, punctuation, quote or dash. Then it removes any falsy items like empty stings or null values. Finally it sorts the array alphabetically and returns the array of strings.
The function mostFrequentWord creates a new array of words and sets it to the result of getTokens, while sending a raw string called text as an argument. The getTokens function takes the string and returns a sorted formatted array of words. Next we create an object to track wordFrequencies. Then iterate through the the words array, adding words to the wordfrequencies object - tracking how often they appear. If the word was already in the wordFrequencies object then we just increment its frequency value, otherwise we add the word to the object with a frequency of 1.
Now that we have the wordFrequency data object, we need to compare the values and determine the most frequent word. We create some variables to track