I don't like how this function uses one argument and two variables from the containing context. It looks to me like you want a generic insertAt() utility function. So write one and pass all the arguments you need.