It works when there are only two elements in the array. How come the third one causes a NaN?