JavaScript implementation of Kadane algorithm