Skip to content

Instantly share code, notes, and snippets.

@st32lthx
st32lthx / memoization.js
Created November 4, 2015 02:36
Simple Memoization Example in JavaScriopt
var fibo = (function () {
var memo = {};
function fi(n) {
if (n < 0) { return -1 } else {
var value = (n in memo) ? memo[n] : (!n || n === 1) ? 1 : fi(n - 1) + fi(n - 2);
memo[n] = value;
return value;
}
}
return fi;
@st32lthx
st32lthx / basic_promise_err_handle.js
Last active August 29, 2015 14:09
Promises Basics in JavaScript
// add some basic error handling
var q = require("q");
var fs = require("fs");
function qread(file) {
var _q = q.defer();
fs.readFile(file, "utf-8", function (err, data) {
if (err) { _q.reject(new Error(err))
} else { _q.resolve(data); }
});
return _q.promise;
@st32lthx
st32lthx / 0_reuse_code.js
Created January 4, 2014 03:52
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console