Arrays help us store lists of things. Common operations let us add, remove, or find items easily.
Common array operations in Javascript
class SimpleArray { constructor() { this.items = []; } add(item) { this.items.push(item); } remove(item) { const index = this.items.indexOf(item); if (index !== -1) { this.items.splice(index, 1); } } contains(item) { return this.items.includes(item); } size() { return this.items.length; } print() { console.log(this.items); } }
The push method adds an item to the end of the array.
The splice method removes items by index.
const myArray = new SimpleArray(); myArray.add('apple'); myArray.print();
const myArray = new SimpleArray(); myArray.remove('apple'); myArray.print();
const myArray = new SimpleArray(); myArray.add('apple'); myArray.add('banana'); myArray.remove('apple'); myArray.print();
const myArray = new SimpleArray(); myArray.add('apple'); console.log(myArray.contains('apple')); console.log(myArray.contains('banana'));
This program shows how to add, check, and remove items from an array wrapped in a class. It prints the array before and after changes.
class SimpleArray { constructor() { this.items = []; } add(item) { this.items.push(item); } remove(item) { const index = this.items.indexOf(item); if (index !== -1) { this.items.splice(index, 1); } } contains(item) { return this.items.includes(item); } size() { return this.items.length; } print() { console.log(this.items); } } const fruits = new SimpleArray(); console.log('Initial array:'); fruits.print(); fruits.add('apple'); fruits.add('banana'); fruits.add('cherry'); console.log('After adding items:'); fruits.print(); console.log('Contains banana?', fruits.contains('banana')); console.log('Contains grape?', fruits.contains('grape')); fruits.remove('banana'); console.log('After removing banana:'); fruits.print(); fruits.remove('grape'); console.log('After trying to remove grape (not in list):'); fruits.print();
Adding an item with push is fast, O(1) time.
Removing an item requires finding it first, so it takes O(n) time where n is the array size.
Common mistake: Trying to remove an item not in the array does nothing, so always check if it exists if needed.
Use contains to check presence before removing if you want to avoid silent failures.
Arrays store lists of items in order.
Use push to add items at the end.
Use indexOf and splice to remove items by value.
Use includes to check if an item is in the array.
Practice
Solution
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.Step 2: Identify the method that adds to the end
push() is the method that adds new items at the end of the array.Final Answer:
push() -> Option CQuick Check:
push() adds item at end [OK]
- Confusing push() with pop()
- Using shift() to add items
- Using unshift() to add at end
arr?Solution
Step 1: Recall array methods for removing items
pop() removes last item, shift() removes first item, push() adds last, unshift() adds first.Step 2: Choose method that removes first item
shift() removes the first item from the array.Final Answer:
arr.shift() -> Option AQuick Check:
shift() removes first item [OK]
- Using pop() to remove first item
- Using push() or unshift() to remove items
- Confusing shift() with unshift()
const fruits = ['apple', 'banana', 'cherry'];
const index = fruits.indexOf('banana');
if (index !== -1) {
fruits.splice(index, 1);
}
console.log(fruits);Solution
Step 1: Find the index of 'banana' in the array
indexOf('banana') returns 1 because 'banana' is at position 1.Step 2: Remove 'banana' using splice
splice(1, 1) removes 1 item at index 1, so 'banana' is removed.Final Answer:
['apple', 'cherry'] -> Option DQuick Check:
Removed 'banana' from array [OK]
- Not checking index before splice
- Using splice with wrong index
- Expecting splice to return new array
const colors = ['red', 'green', 'blue'];
const index = colors.indexOf('orange');
colors.splice(index, 1);
console.log(colors);Solution
Step 1: Check indexOf result for 'orange'
Since 'orange' is not in the array, indexOf returns -1.Step 2: Understand splice behavior with negative index
splice(-1, 1) removes the last item, so 'blue' is removed unexpectedly.Final Answer:
indexOf returns -1 if item not found, causing splice to remove last item -> Option AQuick Check:
Check index before splice to avoid removing wrong item [OK]
- Not checking if index is -1
- Using includes() instead of indexOf for index
- Assuming splice can't remove by index
nums = [1, 2, 3, 4, 5], which code snippet correctly removes all even numbers from the array?Solution
Step 1: Understand filtering even numbers
To remove even numbers, keep only numbers where n % 2 !== 0 (odd numbers).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.Final Answer:
nums = nums.filter(n => n % 2 !== 0); -> Option BQuick Check:
filter odd numbers to remove even [OK]
- Using filter condition backwards
- Using splice without index knowledge
- Using pop to remove specific items
Ready to prove it?
Pass the test and this pattern moves unknown
