Array.prototype.spliceLoading saved progress…

Array.prototype.splice

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.

Signature

function arraySplice(arr, start, deleteCount, ...items) {
  // mutates arr in place; returns an array of the removed elements.
}

Examples

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)

Notes

  • Mutates in placearr itself changes; callers rely on that. Don't return a new array.
  • Returns the removed elements — the return value is what came out, not the modified 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.
  • Insert and delete togetheritems go in at start after the removal, so you can replace a slice with any number of new elements.
Loading editor…