Bird
Raised Fist0
Javascriptprogramming~20 mins

Common array operations in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Array Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Array filter and map combination
What is the output of the following JavaScript code?
Javascript
const numbers = [1, 2, 3, 4, 5];
const result = numbers.filter(n => n % 2 === 0).map(n => n * 3);
console.log(result);
A[3, 9, 15]
B[6, 12]
C[2, 4]
D[1, 3, 5]
Attempts:
2 left
πŸ’‘ Hint
Remember filter keeps only even numbers, then map multiplies each by 3.
❓ Predict Output
intermediate
2:00remaining
Result of Array reduce for sum
What is the value of total after running this code?
Javascript
const values = [10, 20, 30];
const total = values.reduce((acc, val) => acc + val, 5);
console.log(total);
A60
BSyntaxError
C30
D65
Attempts:
2 left
πŸ’‘ Hint
The initial value for reduce is 5, so sum starts from 5.
❓ Predict Output
advanced
2:00remaining
Output of Array splice method
What will be logged to the console after running this code?
Javascript
const arr = [1, 2, 3, 4, 5];
const removed = arr.splice(2, 2, 8, 9);
console.log(arr);
console.log(removed);
Aarr: [1, 2, 8, 9], removed: [5]
Barr: [1, 2, 3, 4, 5], removed: []
Carr: [1, 2, 8, 9, 5], removed: [3, 4]
Darr: [8, 9, 5], removed: [1, 2, 3, 4]
Attempts:
2 left
πŸ’‘ Hint
splice removes 2 elements starting at index 2 and inserts 8 and 9 there.
❓ Predict Output
advanced
2:00remaining
Output of Array sort with compare function
What is the output of this code?
Javascript
const fruits = ['banana', 'apple', 'cherry'];
fruits.sort((a, b) => b.localeCompare(a));
console.log(fruits);
A['cherry', 'banana', 'apple']
B['apple', 'banana', 'cherry']
C['banana', 'apple', 'cherry']
DTypeError
Attempts:
2 left
πŸ’‘ Hint
localeCompare returns positive if a is after b alphabetically. b.localeCompare(a) sorts descending.
🧠 Conceptual
expert
2:00remaining
Which option causes a runtime error?
Which of the following array operations will cause a runtime error when executed?
Aconst arr = [1, 2, 3]; arr.forEach(x => { if(x > 1) throw new Error('Stop'); });
Bconst arr = [1, 2, 3]; const filtered = arr.filter(x => x > 1);
Cconst arr = [1, 2, 3]; const result = arr.map(x => x * 2);
Dconst arr = [1, 2, 3]; const first = arr.find(x => x > 3);
Attempts:
2 left
πŸ’‘ Hint
Look for explicit error throwing inside the callback.

Practice

(1/5)
1. Which method adds a new item to the end of a JavaScript array?
easy
A. shift()
B. pop()
C. push()
D. unshift()

Solution

  1. Step 1: Understand array methods for adding/removing items

    push() adds items to the end, pop() removes from the end, shift() removes from the start, unshift() adds to the start.
  2. Step 2: Identify the method that adds to the end

    push() is the method that adds new items at the end of the array.
  3. Final Answer:

    push() -> Option C
  4. Quick Check:

    push() adds item at end [OK]
Hint: Remember push adds at end, pop removes from end [OK]
Common Mistakes:
  • Confusing push() with pop()
  • Using shift() to add items
  • Using unshift() to add at end
2. Which of the following is the correct syntax to remove the first item from an array named arr?
easy
A. arr.shift()
B. arr.unshift()
C. arr.push()
D. arr.pop()

Solution

  1. Step 1: Recall array methods for removing items

    pop() removes last item, shift() removes first item, push() adds last, unshift() adds first.
  2. Step 2: Choose method that removes first item

    shift() removes the first item from the array.
  3. Final Answer:

    arr.shift() -> Option A
  4. Quick Check:

    shift() removes first item [OK]
Hint: shift() removes first, pop() removes last [OK]
Common Mistakes:
  • Using pop() to remove first item
  • Using push() or unshift() to remove items
  • Confusing shift() with unshift()
3. What is the output of the following code?
const fruits = ['apple', 'banana', 'cherry'];
const index = fruits.indexOf('banana');
if (index !== -1) {
  fruits.splice(index, 1);
}
console.log(fruits);
medium
A. ['apple', 'banana', 'cherry']
B. ['banana', 'cherry']
C. ['apple']
D. ['apple', 'cherry']

Solution

  1. Step 1: Find the index of 'banana' in the array

    indexOf('banana') returns 1 because 'banana' is at position 1.
  2. Step 2: Remove 'banana' using splice

    splice(1, 1) removes 1 item at index 1, so 'banana' is removed.
  3. Final Answer:

    ['apple', 'cherry'] -> Option D
  4. Quick Check:

    Removed 'banana' from array [OK]
Hint: indexOf finds position, splice removes item at that position [OK]
Common Mistakes:
  • Not checking index before splice
  • Using splice with wrong index
  • Expecting splice to return new array
4. The following code is intended to remove 'orange' from the array, but it doesn't work as expected. What is the error?
const colors = ['red', 'green', 'blue'];
const index = colors.indexOf('orange');
colors.splice(index, 1);
console.log(colors);
medium
A. indexOf returns -1 if item not found, causing splice to remove last item
B. splice() is used incorrectly; it should be pop()
C. indexOf should be replaced with includes()
D. splice() cannot remove items by index

Solution

  1. Step 1: Check indexOf result for 'orange'

    Since 'orange' is not in the array, indexOf returns -1.
  2. Step 2: Understand splice behavior with negative index

    splice(-1, 1) removes the last item, so 'blue' is removed unexpectedly.
  3. Final Answer:

    indexOf returns -1 if item not found, causing splice to remove last item -> Option A
  4. Quick Check:

    Check index before splice to avoid removing wrong item [OK]
Hint: Check indexOf result before splice to avoid errors [OK]
Common Mistakes:
  • Not checking if index is -1
  • Using includes() instead of indexOf for index
  • Assuming splice can't remove by index
5. Given an array nums = [1, 2, 3, 4, 5], which code snippet correctly removes all even numbers from the array?
hard
A. nums = nums.filter(n => n % 2 === 0);
B. nums = nums.filter(n => n % 2 !== 0);
C. nums.splice(1, 2);
D. nums.pop(); nums.pop();

Solution

  1. Step 1: Understand filtering even numbers

    To remove even numbers, keep only numbers where n % 2 !== 0 (odd numbers).
  2. Step 2: Check each option

    nums.filter(n => n % 2 !== 0) keeps odd numbers, removing all evens. nums.filter(n => n % 2 === 0) keeps evens. nums.splice(1, 2) removes items at fixed indexes. nums.pop() twice removes only the last two items.
  3. Final Answer:

    nums = nums.filter(n => n % 2 !== 0); -> Option B
  4. Quick Check:

    filter odd numbers to remove even [OK]
Hint: Use filter with condition to keep desired items [OK]
Common Mistakes:
  • Using filter condition backwards
  • Using splice without index knowledge
  • Using pop to remove specific items
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard