📖 Scenario: You are managing a list of guests for a party. You want to add a new guest exactly in the middle of the current guest list to keep the seating balanced.
🎯 Goal: Learn how to insert an element into the middle of an array in TypeScript.
📋 What You'll Learn
Create an array called guests with exactly these names: 'Alice', 'Bob', 'Charlie', 'Diana'
Create a variable called newGuest and set it to 'Eve'
Calculate the middle index of the guests array and store it in a variable called middleIndex
Insert newGuest into the guests array at the middleIndex
Print the guests array after insertion
💡 Why This Matters
🌍 Real World
In real life, you might need to add a new item exactly in the middle of a list, like seating guests evenly or inserting a new task in a schedule.
💼 Career
Understanding how to insert elements in arrays is a basic skill for many programming jobs, especially when managing lists or collections of data.
Progress0 / 4 steps
1
Create the initial guest list
Create an array called guests with these exact names: 'Alice', 'Bob', 'Charlie', 'Diana'
DSA Typescript
Hint
Use square brackets [] to create an array and separate names with commas.
2
Add the new guest variable
Create a variable called newGuest and set it to the string 'Eve'
DSA Typescript
Hint
Use const to create a variable and assign the string 'Eve'.
3
Calculate the middle index
Create a variable called middleIndex and set it to the middle index of the guests array using Math.floor(guests.length / 2)
DSA Typescript
Hint
Use Math.floor to get the integer middle index.
4
Insert the new guest and print the list
Use guests.splice(middleIndex, 0, newGuest) to insert newGuest at middleIndex, then print the guests array using console.log(guests)
DSA Typescript
Hint
Use splice to insert without removing elements, then print the array.
Practice
(1/5)
1. What does inserting an element at the middle index of an array mean?
easy
A. Adding the element exactly in the center position of the array
B. Adding the element at the start of the array
C. Adding the element at the end of the array
D. Replacing the middle element with a new element
Solution
Step 1: Understand array indexing
Arrays have positions starting from 0 to length-1. The middle index is usually length/2 (rounded down).
Step 2: Define insertion at middle
Inserting at middle means placing the new element at this middle index, shifting elements after it to the right.
Final Answer:
Adding the element exactly in the center position of the array -> Option A
Quick Check:
Middle insertion = center position [OK]
Hint: Middle index = array length divided by 2 [OK]
Common Mistakes:
Confusing insertion with replacement
Inserting at start or end instead of middle
Not shifting elements after insertion
2. Which of the following TypeScript code snippets correctly inserts the value 99 at the middle index of array arr?
easy
A. arr.unshift(99);
B. arr.push(99);
C. arr.splice(Math.floor(arr.length / 2), 0, 99);
D. arr[ Math.floor(arr.length / 2) ] = 99;
Solution
Step 1: Understand splice method
splice(index, 0, value) inserts value at index without removing elements.
Step 2: Check other options
push adds at end, unshift adds at start, direct assignment replaces element.
Final Answer:
arr.splice(Math.floor(arr.length / 2), 0, 99); -> Option C
Quick Check:
splice with 0 delete count inserts [OK]
Hint: Use splice(index, 0, value) to insert without removal [OK]
Common Mistakes:
Using push or unshift instead of splice
Replacing element instead of inserting
Incorrect index calculation
3. What is the output of the following TypeScript code?
splice coerces 2.5 to integer 2 (ToInteger(2.5) = 2). Inserts 50 at index 2, shifting 3,4,5 right: [1, 2, 50, 3, 4, 5].
Final Answer:
[1, 2, 50, 3, 4, 5] -> Option D
Quick Check:
Float index coerced to 2 [OK]
Hint: Always floor length/2 for middle index [OK]
Common Mistakes:
Confusing with unshift (insert at start)
Confusing with push (append at end)
Inserting at index 3 instead of 2
5. Given an array arr = [5, 10, 15, 20, 25], write a TypeScript function insertMiddle that inserts a given value exactly in the middle of the array. What will be the array after calling insertMiddle(arr, 12)?
hard
A. [5, 10, 12, 15, 20, 25]
B. [5, 10, 15, 12, 20, 25]
C. [12, 5, 10, 15, 20, 25]
D. [5, 10, 15, 20, 25, 12]
Solution
Step 1: Calculate middle index for odd length
arr.length = 5, middle index = Math.floor(5/2) = 2.
Step 2: Insert 12 at index 2
Using splice(2, 0, 12) inserts 12 before element at index 2 (which is 15), shifting elements right.
Final Answer:
[5, 10, 12, 15, 20, 25] -> Option A
Quick Check:
Inserted 12 at middle index 2 [OK]
Hint: Use Math.floor(length/2) for middle insertion [OK]
Common Mistakes:
Inserting at wrong index for odd length
Appending instead of inserting
Replacing element instead of inserting
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.