JS Built-ins
← Array58 / 226
ArrayArray.prototype.fillbeginner

Array.prototype.fill() β€” fill from index

πŸ“– Usage Example
const arr = [1, 2, 3, 4, 5]
arr.fill(9, 2)  // β†’ [1, 2, 9, 9, 9]
// fills from index 2 to the end

Pass a start index to fill() to replace only elements from that position onwards.

Array.prototype.fill() β€” with start index

The second argument to fill() specifies the start index. Elements before that index are left unchanged.

Challenge: Implement fillFrom(arr, val, start) that fills from start to the end of the array with val.

fillFrom([1, 2, 3, 4, 5], 0, 2) // β†’ [1, 2, 0, 0, 0]
fillFrom([1, 2, 3], 9, 1)       // β†’ [1, 9, 9]
πŸ’‘ Hints (2)
  • β€’ `arr.fill(val, start)` fills from `start` up to (and including) the last element.
  • β€’ Elements at indices 0 through `start - 1` are left untouched.