Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
alexhawkins / es6ControllerComponent.es6
Created June 24, 2015 17:42
Shipment Details Module, Controller, Routes, Services with ES6
/************************** MODULE ***********************************************************************/
'use strict';
/*jshint esnext: true */
import ShipmentDetailsRoute from './shipment-details.routes';
import ShipmentDetailsCtrl from './shipment-details.controller';
@alexhawkins
alexhawkins / goodControllerStyle.js
Last active January 6, 2016 05:03
Sample Controller and Service using ES6 and John Papa Style Guide
(() => {
'use strict';
/*************************************************************
* @ngdoc controller
* @name dashboard.customer.controller:CustomerCtrl
*
* @description
*
* CustomerCtrl Class for Customer Model
@alexhawkins
alexhawkins / dsClosures.js
Created March 3, 2015 06:50
Building Data Structures with closures
//Building Data Structures with closures
//What's the plan? Build some data structures without using assignment,
//then building some functions which operate on those data structures.
//Why? Exploring what we can do with the closure property, and to get a better
//understanding of how it works.
/*
cons, first, rest, list, head, tail, length, each,
@alexhawkins
alexhawkins / hilight.js
Created February 28, 2015 05:40
Recursive HiLight vs JQuery HiLight
//RECURSIVE HI-LIGHT
function highlight(word) {
var matchExp = new RegExp(word, 'g');
var highlightExp = '<span class="highlight">' + word + '</span>';
var traverseDom = function(tree) {
for (var i = 0; i < tree.children.length; i++) {
var node = tree.children[i];
traverseDom(node);
tree.innerHTML = tree.innerHTML.replace(matchExp, highlightExp);
@alexhawkins
alexhawkins / superHashTable.js
Last active March 29, 2016 18:21
Murmur3 Hash Table in JavaScript
/**
* MURMUR 3 HASH TABLE WITH OBJECT HASHING
*
* A hash table with `insert()`, `retrieve()`, and `remove()` methods.
* It handles hashing collisions correctly. And doubles the storage
* limit as soon as the total number of items stored is greater than
* 3/4th of the number of slots in the storage array.
* It resizes by half whenever utilization drops below 1/4.
* It also caches insertions at runtime for even faster retrieval.
*/
@alexhawkins
alexhawkins / kNodesInBST.js
Created February 19, 2015 10:12
How to find K Nodes at any level in a Binary Search Tree
/*Once you get your data into a binary search tree, you can recurse it like below.
Use Depth First Traversal to keep track of the depth at each node in the tree.
On each recursive call, push the node at the current level to a hash table, using the
level as your key and the node as your value.
In JavaScript, you can use an array or an object literal to do this. I'm storing everything
in a JavaScript object literal which is similar to a hash table under the hood. Like this:
level = 0
@alexhawkins
alexhawkins / queueStack.js
Last active March 28, 2019 14:20
Queue using Two Stacks
'use strict';
//implement a queue using two stacks
//create a stack data structure
function Stack() {
var storage = [];
this.push = function() {
storage.push.apply(storage, arguments);
};
@alexhawkins
alexhawkins / balancedBinaryTree.js
Created February 18, 2015 05:01
Balancing A Binary Tree
/* Write a funciton to check to see if a binary Tree is balanced.
For the purposes of this tree, a balanced tree is defined to be a
tree such that the heights of the subtrees of any node never differ
by more than one */
BinaryTree.prototype.isBalanced = function() {
var checkHeight = function(current) {
if (!current) return 0 //height = 0;
@alexhawkins
alexhawkins / binaryTree.js
Last active January 5, 2025 14:48
Binary Trees and Tree Traversal
'use strict';
function BinarySearchTree() {
this.root = null;
}
BinarySearchTree.prototype.makeNode = function(value) {
var node = {};
node.value = value;
node.left = null;
@alexhawkins
alexhawkins / codeChallenges.js
Last active August 29, 2015 14:15
Code Challengees
//1) reverse a string without using any variables or
/*****************************************************/
function reverseString(s) {
if (s === '')
return '';
else
return s.slice(1) + s[0];
}