Challenge 1 +15 XP
Complete the function to remove exactly one element at index 2 without leaving a hole, using splice.
- Test 1 — expects "10,20,40\n"
Show solution (0 XP)
const arr = [10, 20, 30, 40];
arr.splice(2, 1);
console.log(arr.join(',')); You already know that push() and pop() modify the end of an array. But what if you want to modify the beginning?
The shift() method removes the first element from an array and returns it, shifting all other elements down by one index.
The unshift() method adds one or more elements to the beginning of an array and returns the new length.
const arr = [10, 20, 30];
console.log(arr.shift());
console.log(arr[0]);10 20
const nums = [2, 3];
console.log(nums.unshift(1));
console.log(nums[0]);3 1
While push and pop are very fast (O(1)), shift and unshift are much slower (O(N)) because every single item in the array must have its index updated. For small arrays, you won’t notice a difference. But for very large arrays (e.g., 100,000 items), using shift inside a loop can severely impact performance.
Here’s a diagram showing why shift requires renumbering:
spliceThe splice() method lets you add, remove, or replace elements anywhere in the array. It modifies the array in place and returns an array containing the deleted items.
The syntax is: array.splice(startIndex, deleteCount, item1, item2, ...).
const letters = ['a', 'b', 'c', 'd'];
// Start at index 1, remove 2 items, and insert 'z'
const removed = letters.splice(1, 2, 'z');
console.log("Array is now:", letters.join(','));
console.log("Removed items:", removed.join(','));Array is now: a,z,d Removed items: b,c
sliceThe slice() method looks similarly named to splice(), but it behaves very differently. It returns a shallow copy of a portion of an array into a new array object. It does not modify the original array!
The syntax is: array.slice(startIndex, endIndex). Note that the endIndex is exclusive (it extracts up to, but not including, the endIndex).
const colors = ['red', 'green', 'blue', 'yellow'];
// Start at index 1, stop BEFORE index 3
const subColors = colors.slice(1, 3);
console.log("Extracted:", subColors.join(','));
console.log("Original:", colors.join(','));Extracted: green,blue Original: red,green,blue,yellow
slice vs splice QuizBecause these two methods have such similar names, they are constantly confused by beginners. Remember this golden rule: splice mutates, slice copies.
Can you predict what these two arrays will contain after the following operations?
Read the code. What exactly will it print? Commit to an answer before you look.
const arr1 = [1, 2, 3, 4];
arr1.slice(1, 3);
const arr2 = [1, 2, 3, 4];
arr2.splice(1, 2);
console.log(arr1.join(','));
console.log(arr2.join(','));1,2,3,4 1,4
If you got that wrong, don’t worry—almost everyone does the first time! arr1 remains completely unchanged because slice just created a new array and we didn’t save it anywhere. arr2 was mutated by splice, so the middle elements were destroyed.
const arr = [1, 2, 3];
const copy = arr.slice();
arr.splice(1, 1);Does the slice method modify the original array?
No, it creates a shallow copy of a portion of the array — slice() returns a new array and leaves the original array completely untouched.
What does the splice method do?
It changes the contents of an array by removing or replacing existing elements and/or adding new elements in place — splice() is a versatile method that can add, remove, and replace items, modifying the array in place.
Why might shift() be slower than pop() for large arrays?
shift() requires re-indexing every other element in the array — When you remove the first element (index 0), all subsequent elements must be shifted left by one index to fill the gap, which is an O(N) operation.
If you call shift() on an empty array, what happens?
It returns undefined — Like pop(), calling shift() on an empty array simply returns undefined.
Complete the function to remove exactly one element at index 2 without leaving a hole, using splice.
const arr = [10, 20, 30, 40];
arr.splice(2, 1);
console.log(arr.join(',')); Bug Hunt: This code is trying to print the items starting from index 1 up to index 3 (inclusive, so 3 items). But it only prints two! Fix it.
This code runs. It just does the wrong thing. Read it, find the defect, fix it — the tests below decide when you are right.
const words = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
const subset = words.slice(1, 4);
console.log(subset.join(','));