The fill() method changes all elements in an array to a static value, from a start index to an end index.
β Implementation
Array.prototype.customFill = function(value, start = 0, end = this.length) { // Adjust negative indices for start and end start = start < 0 ? Math.max(this.length + start, 0) : start; end = end < 0 ? Math.max(this.length + end, 0) : end; // Fill the array with the given value from start to end (end is exclusive) for (let i = start; i < end; i++) { this[i] = value; } return this; // Return the modified array since fill modifies it in place }; // Example usage: const array1 = [1, 2, 3, 4]; console.log(array1.customFill(0, 2, 4)); // Output: [1, 2, 0, 0] console.log(array1.customFill(5, 1)); // Output: [1, 5, 5, 5] console.log(array1.customFill(6)); // Output: [6, 6, 6, 6]
How It Works:
- Parameters :
value: The value to fill the array with.start: The index to begin filling from (defaults to 0).end: The index to stop filling at (non-inclusive, defaults to the arrayβs length).- Negative Indices :
If
startorendare negative, they are adjusted to count from the end of the array. - Filling the Array :
A
forloop iterates fromstarttoend(not includingend) and fills the array withvalue. - In-Place Modification :
Since
.fill()modifies the array directly,customFillreturns the modified array.
Key Features:
- Supports negative indices by adjusting them to count from the end of the array.
- Modifies the array in place and returns the updated array reference (like
.fill()). - Defaults to
start = 0andend = array.lengthif not provided.
Quick Quiz
Test your understanding with 3 quick questions
Q1What does `[1, 2, 3, 4].customFill(0, 1, 3)` return?
Q2Does `fill()` modify the original array or create a new one?
Q3What does `[1, 2, 3].customFill(5, -2)` do?