Skip to content

Instantly share code, notes, and snippets.

@biyootiful
Last active August 22, 2017 19:18
Show Gist options
  • Save biyootiful/060973f5afcfa81c4ee136fcd6f9658b to your computer and use it in GitHub Desktop.
Save biyootiful/060973f5afcfa81c4ee136fcd6f9658b to your computer and use it in GitHub Desktop.
Drills
function getTokens(rawString) {
// NB: `.filter(Boolean)` removes any falsy items from an array
return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
}
function mostFrequentWord(text) {
//filter out using lowercase, split w/ regex, etc ...
const words = getTokens(text);
//create an empty obj in the scope
const wordFrequencies = {};
//loop through "words" and assigns a key(a word) and value (number) to the const "wordFrequencies",
//and any words will start with a value of '1' and incremented if found.
for (let i = 0; i <= words.length; i++) {
if (words[i] in wordFrequencies) {
wordFrequencies[words[i]]++;
}
else {
wordFrequencies[words[i]]=1;
}
}
//Variable currentMaxKey gets the first word of wordFrequencies
//Variable currentMaxCount gets first word's value(count)
let currentMaxKey = Object.keys(wordFrequencies)[0];
let currentMaxCount = wordFrequencies[currentMaxKey];
//validate if currentMaxKey is the most counted by looping through the items in wordFrequencies
//if the word value(count) is greater than the most counted(num), switch maxKey & Count.
//However, this validation does not work if there's two items with the same number of maxcount,
//(eg. "item2" :12 <-highest count, "item4":12, it will just return "item2")
for (var word in wordFrequencies) {
if (wordFrequencies[word] > currentMaxCount) {
currentMaxKey = word;
currentMaxCount = wordFrequencies[word];
}
}
//finally output currentMaxKey
return currentMaxKey;
}
//creating arr
https://repl.it/G9GB/603
//pushMutable
https://repl.it/G9GI/612
//arrIndex
https://repl.it/G9GO/425
//length
https://repl.it/G9GS/505
//slice
https://repl.it/G9GV/618
https://repl.it/G9G7/878
//map
https://repl.it/G9HI/618
//sort
https://repl.it/G9H5/465
//filter
https://repl.it/G9Hd/473
//find
https://repl.it/G9Hl/395
//forLoop
https://repl.it/G9KJ/965
https://repl.it/G9KO/659
https://repl.it/G9KW/1008
https://repl.it/Icy2/840
https://repl.it/GiQ8/1965
//areaCompute
https://repl.it/G9Do/384
// celsFahrConverter
https://repl.it/G9EI/455
//divisible
https://repl.it/KSq3/0
//pt1
https://repl.it/G9Lb/566
https://repl.it/G9Lf/445
https://repl.it/G9Lj/537
https://repl.it/G9Lo/344
//pt2
https://repl.it/G9NG/913
https://repl.it/G9N6/727
https://repl.it/G9OF/599
https://repl.it/G9OV/984
What is scope? Your explanation should include the idea of global vs. local scope.
- In computer programming scope is a region of the program where a variable defined within the region is visible or accessible.
In javascript there are two types of scopes, local and global. Variables defined within a function is in "local scope" and
they are accessible inside that function, while variables in "global scope" are defined outside of a function body and
will live throughout runtime (and accessible to any scope). Under the hood, javascript uses scope chain method, which looks up
for variables it needs in the current scope and if fails, then it looks up its parent scope and so on.
This wasn't covered in the course but a couple things I feel like it's worth mentioning are that block statements (if, while, ... )
do not create a scope and that context(object, "this") is different from scope(within function).
Why are global variables avoided?
- They cause unintended side-effects and that leads to your code hard to reason about. (indeterminate)
Explain JavaScript's strict mode
- 'strict mode', introduced in ES5, helps developers to write more stable code. It does lot more than what's covered here but here are
the list of features that I found helpful.
1. 'strict mode' makes it impossible to accidentally create global variables (foo = "something" //not allowed),
2. it makes assignments which would otherwise silently fail (eg. NaN is non-writable variable)
3. it throws an error when you try to delete something that's not supposed to be deleted. (before it would just silently have no effect)
4. you can define it in a function. (function something(){ 'use strict' ... })
What are side effects, and what is a pure function?
- A pure function doesn’t rely on and does not modify the states of variables out of its scope.
A side effect is any effect other than that the return value.
function pureFunction ( a, b ) {
return a + b ;
}
fucntion impureFunction ( a, b ) {
a = iamSideEffect(a);
return a + b;
}
//wisePerson
https://repl.it/G8oY/897
//shouter
https://repl.it/G8sD/691
//textNormalizer
https://repl.it/G8sU/540
//just a snippet for 1st exercies
function doTrafficLights() {
const activeLight = getActiveLight();
(activeLight === 'red') && turnRed();
(activeLight === 'green') && turnGreen();
(activeLight === 'yellow') && turnYellow();
}
//2nd
function main() {
try {
doAllTheThings();
} catch(error) {
console.error;
reportError(error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment