I am an absolute beginner but... l see that after the for loop, (i) is the same as (n), you can delete it.
also, l think you forgot to delete the (dates) array.