Array.prototype.splice is the array Swiss-army knife: it removes, inserts, or replaces elements in place, at any position. It's the one array method that both mutates the original and returns something — the elements it removed.
Implement arraySplice(arr, start, deleteCount, ...items). Starting at index start, remove deleteCount elements, then insert items in their place, changing arr directly. Return an array of the removed elements. start and deleteCount clamp to the array's bounds, and a negative start counts from the end.
function arraySplice(arr, start, deleteCount, ...items) {
// mutates arr in place; returns an array of the removed elements.
}
const a = [1, 2, 3, 4];
arraySplice(a, 1, 2); // returns [2, 3]; a is now [1, 4]
const b = [1, 2, 3];
arraySplice(b, 1, 0, 'x'); // returns []; b is now [1, 'x', 2, 3] (pure insert)
const c = [1, 2, 3, 4];
arraySplice(c, 2); // returns [3, 4]; c is now [1, 2] (deleteCount omitted)
arr itself changes; callers rely on that. Don't return a new array.start — negative counts from the end (len + start); clamp the result to [0, len].deleteCount omitted — remove everything from start to the end.deleteCount clamps — to [0, len - start]; a negative count removes nothing.items go in at start after the removal, so you can replace a slice with any number of new elements.