Skip to content

Instantly share code, notes, and snippets.

View dnasca's full-sized avatar

Derrik dnasca

View GitHub Profile
@dnasca
dnasca / findingandpushingastringtoanarray.js
Created September 24, 2015 02:53
finding and pushing a string to an array
var text = "This string contains the name Derrik";
var myName = "Derrik";
var hits = [];
for(var i = 0; i < text.length; i++) {
if (text[i] === 'D') {
for(var j = i; i > (myName.length + i); j++) {
hits.push(text[j]);
@dnasca
dnasca / Preferences.sublime-settings
Created September 19, 2015 23:56
general sublime config
{
"ignored_packages":
[
"Vintage"
],
//theme stuff
"color_scheme": "Packages/User/SublimeLinter/Oceanic Next (SL).tmTheme",
"theme": "Broceanic.sublime-theme",
//general
@dnasca
dnasca / asyncCB_es2015.js
Created September 18, 2015 07:45
async cb w/ promises in es2015!
//the most basic async cb
sleep = function(ms) {
let promise = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve("the result");
}, ms);
});
return promise;
};
@dnasca
dnasca / asyncCB_es5.js
Last active September 18, 2015 07:46
the most basic example of an async cb in es5 (the old way)
//the most basic async cb in es5
var sleep = function(ms) {
var promise = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve("the result");
}, ms);
});
return promise;
@dnasca
dnasca / fatArrows.js
Created September 17, 2015 12:51
ES2015 Fat Arrow Function
let arr = [4, 9, 14];
// the old way
let newArr = arr.map(function (value) {
return value + 1;
});
// the new way - ES2015 (3 examples of the same function, each better than the one before it)
let newArr = arr.map( (value) => { return value +1; });
arr.map( value => { return value + 1; });
@dnasca
dnasca / isNegativeZero.js
Created August 20, 2015 23:02
Testing for negative zero.
function isNegativeZero(x) {
return x === 0 && (1/x < 0);
}
@dnasca
dnasca / heapAndStack.js
Last active September 8, 2015 07:28
a contrived example of whats happening with the heap and stack. watch the call stack.
var heap = {};
var stack = [];
var callFunction = function (nameFunc, args, lineNumber) {
var func = heap[nameFunc];
var stackFrame = {
funcy: func,
name: nameFunc,
args: args,
env: {},
@dnasca
dnasca / pattern_iterationAndApplying.js
Created July 29, 2015 06:16
Pattern: Iteration and Applying - applying a callback function to multiple[each] item in a list
// create a function called forEach that iterates over all items in a list and calls a callback for each item
// note: this concept is attached to the array object automatically. this is just to illustrate what is happening under the hood
items = ['one', 'two', 'three'];
//iterator function 1
forEach = function (array, func) { //func = callback function that is passed in (the iterator!)
for (var i = 0; i < array.length; i++) {
func(array[i], i); //item in array, and index item in array
}
@dnasca
dnasca / pattern_asynchronousCallback.js
Created July 29, 2015 05:59
the asynchronous callback -- the most absolute basic example using the setTimeout and setInterval functions on the client
whenDone = function () {
console.log ("done")
};
doWork = function(callback) {
//do some work
setTimeout(callback, 3000); //put the call back on the event loop by 3 seconds (during this time, other work is being done)
};
@dnasca
dnasca / pattern_synchronousCallback.js
Last active August 29, 2015 14:26
Pattern: the synchronous callback -- the callback that is called at the same time [in the same processing cycle] that the function is called.
/*
the synchronous callback:
create a function that can be called at a later time whenever the
higher order function (the function that were passing this function into -- aka: the function doing the calling)
is ready to call/invoke this function. Read this a few times and it will eventually make sense :)
*/
whenDone = function () {
console.log ("done")
};