Skip to content

Instantly share code, notes, and snippets.

function shouter(whatToShout) {
return `${whatToShout.toUpperCase()}!!!`;
}
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
function computeArea(width, height) {
return width * height;
}
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
@elliotkim916
elliotkim916 / Error Alert Drill
Created September 9, 2017 00:03
Logic Drills
function main() {
try {
doAllTheThings();
}
catch(e) {
console.error(e);
reportError(e);
}
}
@elliotkim916
elliotkim916 / Accessing Array Items
Created September 10, 2017 02:31
Array Basic Drills
function accessFirstItem(array) {
return array[0];
}
function accessThirdItem(array) {
return array[2];
}
@elliotkim916
elliotkim916 / Array Copying Drill 1
Created September 12, 2017 03:47
Array Method Drills
function firstFourItems(array) {
return array.slice(0, 4);
}
function lastThreeItems(array) {
return array.slice(-3);
}
Variable scope can be defined as how the variables you declare either can or cannot be accessed at different points in your code.
With global scope, any variable that is declared outside of a Javascript function has global scope.
Inside Javascript functions are local scopes and with the scope chain, Javascript follows the scope chain to determine the value of the variable.
Global variables are avoided because they tend to make unintended side effects more likely.
Also, global variable along with unintended side effects almost guarantee that the code becomes indeterminate, meaning
given a single set of inputs, returns one value sometimes and another at other times.
Whenever Javascript's strict mode is enabled, anytime a variable is declared without const or let, an error will be triggered.
@elliotkim916
elliotkim916 / Deleting Keys
Created September 19, 2017 00:46
Object Drills
function keyDeleter(obj) {
delete obj.foo;
delete obj.bar;
return obj;
}
var sampleObj = {
foo: 'foo',
@elliotkim916
elliotkim916 / FizzBuzz
Created October 12, 2017 01:59
Arrays and Loops Drills
function fizzBuzz(countTo) {
// your code here
var values = [];
for (var i = 1; i <= countTo; i++) {
if (i % 5 === 0 && i % 3 === 0) {
values.push('fizzbuzz');
}
else if (i % 5 === 0) {
values.push('buzz');
}
const studentData = [
{
name: 'Tim',
status: 'Current student',
course: 'Biology'
},
{
name: 'Sue',
status: 'Withdrawn',
course: 'Mathematics'
@elliotkim916
elliotkim916 / Grokking
Last active October 15, 2017 03:26
Analyze a most frequent word program
function getTokens(rawString) {
// NB: `.filter(Boolean)` removes any falsy items from an array
return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
}
The getTokens function is a regular expression which is a sequence of characters that forms a search pattern.
The lower case method will return all the words in text in lower case form.
The regular expression is being used to search for all the punctuation marks in text such as [,!.";:-]+ and
is then using the split method to split the array into an array of substrings, returning a new array.
As noted above, the .filter(Boolean) is being used to remove any falsy items from an array.