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.