Classic algorithms. JavaScript implementation.