Algorithms in JS