📖 Scenario: You are managing a list of daily tasks. Sometimes, you need to remove the task that is exactly in the middle of the list to keep your schedule balanced.
🎯 Goal: Build a program that deletes the middle task from an array of tasks and shows the updated list.
📋 What You'll Learn
Create an array called tasks with exactly these strings: 'Email', 'Meeting', 'Coding', 'Review', 'Lunch'
Create a variable called middleIndex that holds the middle index of the tasks array
Remove the task at middleIndex from the tasks array
Print the updated tasks array
💡 Why This Matters
🌍 Real World
Managing lists like tasks, shopping items, or messages often requires removing items from the middle to keep things balanced or updated.
💼 Career
Understanding how to manipulate arrays by removing elements at specific positions is a fundamental skill for software developers working with data collections.
Progress0 / 4 steps
1
Create the tasks array
Create an array called tasks with these exact strings in order: 'Email', 'Meeting', 'Coding', 'Review', 'Lunch'
DSA Typescript
Hint
Use square brackets [] to create an array and separate the strings with commas.
2
Find the middle index
Create a variable called middleIndex that holds the middle index of the tasks array using Math.floor(tasks.length / 2)
DSA Typescript
Hint
Use Math.floor to get the integer middle index.
3
Remove the middle task
Use tasks.splice(middleIndex, 1) to remove the task at middleIndex from the tasks array
DSA Typescript
Hint
The splice method removes elements from an array starting at the given index.
4
Print the updated tasks array
Print the tasks array using console.log(tasks) to show the updated list after deletion
DSA Typescript
Hint
Use console.log to print the array to the console.
Practice
(1/5)
1. What does deleting the middle element from an array mean? Example: [1, 2, 3, 4, 5] Delete the middle element.
easy
A. Remove the element at index 2 (value 3) from the array
B. Remove the first element (value 1) from the array
C. Remove the last element (value 5) from the array
D. Remove all elements except the middle one
Solution
Step 1: Identify the middle index of the array
The array has 5 elements, so middle index = floor(5/2) = 2.
Step 2: Remove the element at the middle index
Removing element at index 2 removes the value 3 from the array.
Final Answer:
Remove the element at index 2 (value 3) from the array -> Option A
Quick Check:
Middle index = 2, remove element 3 [OK]
Hint: Middle index = floor(length/2), remove that element [OK]
Common Mistakes:
Removing first or last element instead of middle
Using length/2 without floor for odd length
Confusing index with value
2. Which TypeScript code correctly deletes the middle element from array arr?
easy
A. arr.removeAt(Math.floor(arr.length / 2));
B. arr.splice(Math.floor(arr.length / 2), 1);
C. arr.delete(Math.floor(arr.length / 2));
D. arr.pop(Math.floor(arr.length / 2));
Solution
Step 1: Identify correct method to remove element by index in TypeScript
TypeScript arrays use splice(index, count) to remove elements at a specific index.
Step 2: Use Math.floor(arr.length / 2) to get middle index
This expression correctly calculates the middle index for deletion.
Final Answer:
arr.splice(Math.floor(arr.length / 2), 1); -> Option B
Quick Check:
splice + floor middle index = correct deletion [OK]
Hint: Use splice with floor(length/2) to delete middle [OK]
Common Mistakes:
Using non-existent methods like delete or removeAt
C. Using arr.length / 2 without floor causes wrong index
D. splice removes all elements, not one
Solution
Step 1: Check middle index calculation
arr.length / 2 = 4 / 2 = 2 (number), but for even length arrays, middle index should be floor(4/2) = 2, so this seems correct but in TypeScript splice expects integer index.
Step 2: Verify if index is integer
arr.length / 2 is 2 (integer), so no error here. But if length was odd, it would be float. The main issue is that for even length arrays, middle is ambiguous; usually floor is used to pick index 2.
Step 3: Identify actual problem
In this code, no floor is used but division results in integer 2, so no error. However, if length was odd, index would be float causing error. So best practice is to use Math.floor to ensure integer index.
Final Answer:
Using arr.length / 2 without floor causes wrong index -> Option C
Quick Check:
Always floor middle index for splice [OK]
Hint: Always floor middle index before splice [OK]
Common Mistakes:
Not flooring index causing float error
Thinking splice removes all elements
Misunderstanding console.log usage
5. Given an array arr of even length, how can you delete the lower middle element? Example: arr = [2, 4, 6, 8] After deletion, output should be [2, 6, 8].
hard
A. arr.splice(Math.floor(arr.length / 2) - 1, 1);
B. arr.splice(Math.ceil(arr.length / 2), 1);
C. arr.splice(arr.length / 2, 1);
D. arr.splice(arr.length / 2 + 1, 1);
Solution
Step 1: Understand middle elements in even length array
For even length 4, middle indices are 1 and 2 (0-based). Lower middle is index 1.
Step 2: Calculate index to delete
Math.floor(4/2) = 2, subtract 1 to get lower middle index = 1.
Step 3: Use splice to delete element at index 1
arr.splice(1, 1) removes element 4, resulting in [2, 6, 8].
Final Answer:
arr.splice(Math.floor(arr.length / 2) - 1, 1); -> Option A
Quick Check:
Lower middle index = floor(length/2)-1 [OK]
Hint: Lower middle = floor(length/2) - 1 for even arrays [OK]
Common Mistakes:
Deleting upper middle instead of lower middle
Not adjusting index for zero-based arrays
Using ceil instead of floor
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.