Implementing various algorithms in JavaScript