Challenge - 5 Problems
Array Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
What is the output of this code modifying an array with splice?
Consider the following JavaScript code that modifies an array using
splice. What will be logged to the console?Javascript
const arr = [10, 20, 30, 40, 50]; arr.splice(2, 2, 25, 35); console.log(arr);
Attempts:
2 left
π‘ Hint
Remember that splice(start, deleteCount, items...) removes deleteCount items starting at start, then inserts items.
β Incorrect
The splice call removes 2 elements starting at index 2 (30 and 40), then inserts 25 and 35 at that position. The resulting array is [10, 20, 25, 35, 50].
β Predict Output
intermediate2:00remaining
What is the output after using push and pop on an array?
Look at this code that modifies an array with
push and pop. What will be the final output?Javascript
const fruits = ['apple', 'banana']; fruits.push('cherry'); const popped = fruits.pop(); console.log(fruits, popped);
Attempts:
2 left
π‘ Hint
push adds an item to the end, pop removes the last item and returns it.β Incorrect
After pushing 'cherry', the array is ['apple', 'banana', 'cherry']. Then pop removes 'cherry' and returns it. The array is back to ['apple', 'banana'].
π§ Debug
advanced2:00remaining
Why does this code throw an error when modifying an array?
This code tries to modify an array but throws an error. What is the cause?
Javascript
const arr = Object.freeze([1, 2, 3]); arr[0] = 10; console.log(arr);
Attempts:
2 left
π‘ Hint
Object.freeze makes an object immutable.
β Incorrect
The array is frozen, so trying to assign a new value to an element causes a TypeError.
β Predict Output
advanced2:00remaining
What is the output after using map and modifying the original array?
What will be logged after running this code that modifies an array inside a map callback?
Javascript
const nums = [1, 2, 3]; const result = nums.map((num, i, arr) => { arr[i] = num * 2; return num + 1; }); console.log(result, nums);
Attempts:
2 left
π‘ Hint
The map callback modifies the original array while returning a new array.
β Incorrect
The map returns each original number plus 1, so result is [2,3,4]. Meanwhile, the original array elements are doubled inside the callback, so nums becomes [2,4,6].
π§ Conceptual
expert2:00remaining
Which option correctly removes all occurrences of a value from an array?
You want to remove all occurrences of the number 3 from an array. Which code snippet correctly does this without mutating the original array?
Attempts:
2 left
π‘ Hint
Filter creates a new array with only elements that pass the test.
β Incorrect
Option C returns a new array excluding all 3s without changing the original. Option C mutates the array and removes only the first 3. Option C replaces 3s with null but keeps them. Option C is invalid because pop does not take arguments.