CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

πŸ”² Array.prototype.fill() Polyfill

The fill() method changes array elements to a static value within a range. Useful for initializing arrays with default values.

The method changes all elements in an array to a static value, from a start index to an end index. -- βœ… Implementation How It Works: Parameters : : The value to fill the array with. : The index to begin filling from (defaults to 0). : The index to stop filling at (non-inclusive, defaults to the array’s length). Negative Indices : If or are negative, they are adjusted to count from the end of the array. Filling the Array : A loop iterates from to (not including ) and fills the array with . In-Place Modification : Since modifies the array directly, returns 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 ). Defaults to and if not provided. -- <!-quiz-start --Q1: What does return? [ ] [0, 0, 0, 0] [ ] [1, 0, 0, 0] [x] [1, 0, 0, 4] [ ] [0, 1, 2, 3] Q2: Does modify the original array or create a new one? [x] Modifies the original array in place [ ] Creates and returns a new array [ ] Creates a shallow copy first, then modifies it [ ] Depends on the parameters passed Q3: What does do? [ ] Throws an error because of negative index [ ] Fills nothing and returns [1, 2, 3] [x] Fills from index 1 to end, returning [1, 5, 5] [ ] Fills the entire array with 5 <!-quiz-end --
JavaScriptPolyfills
🎯 Array.prototype.at() Polyfill
medium
βœ… Array.prototype.every() Polyfill
medium
πŸ”² Array.prototype.fill() Polyfill
medium
πŸ” Array.prototype.filter() Polyfill
medium
πŸ”Ž Array.prototype.find() Polyfill
medium
πŸ”’ Array.prototype.findIndex() Polyfill
medium
πŸ”™ Array.prototype.findLast() Polyfill
medium
πŸ”™ Array.prototype.findLastIndex() Polyfill
medium
πŸ“‹ Array.prototype.flat() Polyfill
medium
πŸ” Array.prototype.includes() Polyfill
medium
πŸ”’ Array.prototype.indexOf() Polyfill
medium
βœ… Array.isArray() Polyfill
medium
πŸ—ΊοΈ Array.prototype.map() Polyfill
hard
βž– Array.prototype.pop() Polyfill
medium
βž• Array.prototype.push() Polyfill
easy
πŸ”„ Array.prototype.reduce() Polyfill
medium
πŸ”„ Array.prototype.reverse() Polyfill
hard
⬅️ Array.prototype.shift() Polyfill
hard
πŸ”˜ Array.prototype.some() Polyfill
medium
πŸ”€ Array.prototype.sort() Polyfill
hard
➑️ Array.prototype.unshift() Polyfill
hard
πŸ“ž Function.prototype.apply() Polyfill
medium
πŸ”— Function.prototype.bind() Polyfill
hard
πŸ“ž Function.prototype.call() Polyfill
medium
3 of 24
LibraryJavaScriptPolyfills18 of 61

πŸ”² Array.prototype.fill() Polyfill

jspolyfillsmedium

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 start or end are negative, they are adjusted to count from the end of the array.
  • Filling the Array : A for loop iterates from start to end (not including end) and fills the array with value.
  • In-Place Modification : Since .fill() modifies the array directly, customFill returns 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 = 0 and end = array.length if 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?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna