Skip to content

Instantly share code, notes, and snippets.

View kidGodzilla's full-sized avatar
👋
Working from home forever

James Futhey kidGodzilla

👋
Working from home forever
View GitHub Profile
@kidGodzilla
kidGodzilla / letRubyInfluenceJavaScript.js
Last active August 29, 2015 14:12
let rubyInfluenceJavaScript
//let rubyInfluenceJavaScript
Number.prototype.times = function(f){for(a=-1;++a<this;){f(a)}return+this};
Number.prototype.upto = function(a,f){b=this;if(a<b){return+b}for(;b<=a;){f(b++)}return+b};
Number.prototype.downto = function(a,f){b=this;if(a>b){return+b}for(;b>=a;){f(b--)}return+b};
Number.prototype.to = function(a,f){a>this?this.upto(a, f):this.downto(a, f)};
Number.prototype.toS = function(){return ""+this};
String.prototype.toI = function(){return parseInt(this)};
String.prototype.toF = function(){return parseFloat(this)};
// ruby-inspired looping
@kidGodzilla
kidGodzilla / binary-tree.js
Created December 28, 2014 09:04
JavaScript Binary Tree
function BinaryTree () {} (function () {
BinaryTree.prototype = {
// Container for the object's binary tree
tree: null,
// Temporary container when constructing a sorted array
_temp: [],
// Insert a new node into the binary tree
@kidGodzilla
kidGodzilla / self-balancing-binary-tree.js
Created December 28, 2014 10:37
Self-balancing binary tree
function BinaryTree () {} (function () {
BinaryTree.prototype = {
// Container for the object's binary tree
tree: null,
count: 0,
_isBalancing: false,
@kidGodzilla
kidGodzilla / binary-search-tree.js
Created December 29, 2014 08:55
Implementing a Binary Search Tree in JavaScript
function BinaryTree () {} (function () {
BinaryTree.prototype = {
// Container for the object's binary tree
tree: null,
count: 0,
_isBalancing: false,
@kidGodzilla
kidGodzilla / quicksort.js
Created December 30, 2014 09:24
Quicksort in JavaScript
function randomArray() {
for (var a=[], i=0; ++i < 19;) a.push(~~(Math.random() * 100));
return a;
}
function quicksort(a) {
var l = [], r = [], m;
if(!a.length) return [];
@kidGodzilla
kidGodzilla / binary-search.js
Created January 1, 2015 20:56
Fast Binary Search in JavaScript, unminified
function(array, value) {
var j = 0, length = array.length;
// A for loop was used to save space. More easily understood as a while loop. Exits if our pointer moves out of range.
while (j < length) {
var i = (length + j - 1) >> 1; // move the pointer to the median value using a shift in place of Math.floor() (to save characters)
// If the value we're searching for is greater than the median, move our lower-bound past the median
if (value > a[i])
j = i + 1;
// Otherwise, if the value we're searching for is less than the median, move our upper-bound to the median value
else if (value < array[i])
@kidGodzilla
kidGodzilla / insertion-sort.js
Last active March 31, 2018 17:18
Insertion Sort in JavaScript
var insertionSort = function (a) {
// Iterate through our array
for (var i = 1, value; i < a.length; i++) {
// Our array is split into two parts: values preceeding i are sorted, while others are unsorted
// Store the unsorted value at i
value = a[i];
// Interate backwards through the unsorted values until we find the correct location for our `next` value
for (var j = i; a[j-1] > value; j--) {
// Shift the value to the right
a[j] = a[j-1];
@kidGodzilla
kidGodzilla / selection-sort.js
Created January 5, 2015 03:22
Selection Sort in JavaScript
var selectionSort = function (a) {
// Move forward through an array, swapping the value at i with the smallest value after i
for (var i = -1; ++i < a.length;) {
// Move forward from i and remember the position of the smallest value
for (var m = j = i; ++j < a.length;) {
// If the value at j is smaller than our current minimum, remember it's position
if (a[m] > a[j]) m = j;
}
// Swap the value at i with the minimum value following i
@kidGodzilla
kidGodzilla / this-method-will-self-destruct.js
Created January 5, 2015 05:11
Prototype an object with secret methods which self-destruct
function SecretMethods () {} (function () {
var add = function (a,b) {
return a + b;
}
var mul = function (a,b) {
return a * b;
}
@kidGodzilla
kidGodzilla / cachebuster.js
Created May 5, 2015 19:49
JavaScript Cache Buster Example
/**
* Generates a random hex string
*/
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
/**