Challenge - 5 Problems
Array Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after deleting the first element?
Consider the following JavaScript code that deletes the first element of an array. What will be the printed output?
DSA Javascript
let arr = [10, 20, 30, 40]; arr.shift(); console.log(arr.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
The shift() method removes the first element of the array.
✗ Incorrect
The shift() method removes the first element (10) from the array, so the remaining elements are 20, 30, and 40.
❓ Predict Output
intermediateWhat happens if we delete the first element from an empty array?
What will be the output of the following code?
DSA Javascript
let arr = []; arr.shift(); console.log(arr.length);
Attempts:
2 left
💡 Hint
Removing from an empty array does not cause an error.
✗ Incorrect
Calling shift() on an empty array returns undefined but does not change the array length, which remains 0.
❓ Predict Output
advancedWhat is the output after multiple deletions at the beginning?
What will be printed after running this code?
DSA Javascript
let arr = [5, 10, 15, 20, 25]; arr.shift(); arr.shift(); console.log(arr.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Each shift() removes one element from the front.
✗ Incorrect
Two shift() calls remove 5 and then 10, leaving 15, 20, and 25.
❓ Predict Output
advancedWhat error occurs if you try to delete the first element of a non-array?
What will happen when this code runs?
DSA Javascript
let notArray = 12345;
notArray.shift();Attempts:
2 left
💡 Hint
Only arrays have the shift() method.
✗ Incorrect
Since notArray is a number, it does not have the shift() method, causing a TypeError.
🧠 Conceptual
expertWhy is deleting the first element of an array less efficient than deleting the last?
Choose the best explanation for why removing the first element of a JavaScript array is slower than removing the last element.
Attempts:
2 left
💡 Hint
Think about what happens to the indexes of elements after removing the first.
✗ Incorrect
When shift() removes the first element, all remaining elements must be moved one index forward, which takes more time than removing the last element.
