Basic Algorithms written in Javascript