function fn(arr, p, n) { var c = [] for (var i=1; i<p; i++) {c = c.concat(arr.slice())} return c.concat(arr.slice(0,++n)) } alert(fn([1,2,3,4,5], 2, 2)); alert(fn([1,2,3,4,5], 3, 2));