Complete the code to remove the middle element from the array.
const arr = [1, 2, 3, 4, 5]; const mid = Math.floor(arr.length / 2); arr.[1](mid, 1); console.log(arr);
The splice method removes elements from an array at a given index. Here, it removes 1 element at the middle index.
Complete the code to find the middle index correctly for even length arrays.
const arr = [10, 20, 30, 40]; const mid = Math.[1](arr.length / 2); arr.splice(mid, 1); console.log(arr);
For even length arrays, Math.floor gives the lower middle index.
Fix the error in the code to delete the middle element without modifying the original array.
const arr = [5, 6, 7, 8, 9]; const mid = Math.floor(arr.length / 2); const newArr = arr.[1](mid, 1); console.log(newArr);
slice returns a new array and does not modify the original. Using it correctly can create a new array without the middle element.
Fill both blanks to create a new array without the middle element using slice.
const arr = [2, 4, 6, 8, 10]; const mid = Math.floor(arr.length / 2); const newArr = arr.[1](0, mid).concat(arr.[2](mid + 1)); console.log(newArr);
Using slice twice extracts parts before and after the middle index, then concatenates them to form a new array without the middle element.
Fill all three blanks to delete the middle element and print the updated array.
function deleteMiddle(arr: number[]): number[] {
const mid = Math.floor(arr.length / 2);
arr.[1](mid, [2]);
return arr;
}
const nums = [11, 22, 33, 44, 55];
console.log(deleteMiddle(nums)); // [3]The splice method removes 1 element at the middle index. The resulting array excludes the middle element.
