JS Built-ins
← Array59 / 226
ArrayArray.prototype.fillintermediate

Array.prototype.fill() β€” fill a range

πŸ“– Usage Example
const arr = [1, 2, 3, 4, 5]
arr.fill(7, 1, 4)  // β†’ [1, 7, 7, 7, 5]
// fills indices 1, 2, 3

Use fill(value, start, end) to replace elements within a specific index range.

Array.prototype.fill() β€” start and end

With all three arguments, fill(value, start, end) fills only the range [start, end) (end is exclusive). Elements outside this range are preserved.

Challenge: Implement fillRange(arr, val, start, end) that fills from start to end (exclusive) with val.

fillRange([1, 2, 3, 4, 5], 0, 1, 3) // β†’ [1, 0, 0, 4, 5]
fillRange([1, 2, 3, 4, 5], 9, 2, 4) // β†’ [1, 2, 9, 9, 5]
πŸ’‘ Hints (2)
  • β€’ The `end` index is exclusive β€” `fill(val, 1, 3)` fills indices 1 and 2, not 3.
  • β€’ When `start === end`, no elements are filled.