Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert the value 99 at the middle index of the array.
DSA Javascript
const arr = [1, 2, 3, 4]; const mid = Math.floor(arr.length / 2); arr.[1](mid, 0, 99); console.log(arr);
Attempts:
3 left
💡 Hint
Common Mistakes
Using push or pop which add or remove elements only at the end.
Using slice which does not modify the original array.
✗ Incorrect
The splice method inserts elements at a specified index without removing any elements when the delete count is 0.
2fill in blank
mediumComplete the code to find the middle index of the array correctly for insertion.
DSA Javascript
const arr = [10, 20, 30, 40, 50]; const mid = Math.[1](arr.length / 2); arr.splice(mid, 0, 25); console.log(arr);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.ceil which rounds up and may cause index out of expected middle.
Using Math.round which rounds to nearest integer and may not be consistent.
✗ Incorrect
Math.floor returns the largest integer less than or equal to the given number, which correctly finds the middle index for insertion.
3fill in blank
hardFix the error in the code to insert 100 at the middle index of the array.
DSA Javascript
let numbers = [5, 15, 25, 35]; let middle = Math.floor(numbers.length / 2); numbers.[1](middle, 0, 100); console.log(numbers);
Attempts:
3 left
💡 Hint
Common Mistakes
Misspelling splice as splicee.
Using slice which does not insert elements.
✗ Incorrect
The correct method name is splice, not splicee or insert. Slice does not modify the array.
4fill in blank
hardFill both blanks to insert 77 at the middle index of the array without removing any elements.
DSA Javascript
const data = [2, 4, 6, 8, 10]; const midIndex = Math.[1](data.length / 2); data.[2](midIndex, 0, 77); console.log(data);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.ceil which may select the wrong middle index.
Using slice instead of splice which does not modify the array.
✗ Incorrect
Math.floor finds the correct middle index and splice inserts the element without removing any.
5fill in blank
hardFill all three blanks to insert the value 55 at the middle index of the array correctly.
DSA Javascript
let arr = [11, 22, 33, 44]; const middle = Math.[1](arr.length / 2); arr.[2](middle, [3], 55); console.log(arr);
Attempts:
3 left
💡 Hint
Common Mistakes
Using delete count 1 which removes an element.
Using Math.ceil which may select wrong index.
✗ Incorrect
Math.floor finds the middle index, splice inserts without removing elements when delete count is 0.
