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.indexOf() Polyfill

The indexOf() method returns the first index of a value in an array, or -1 if not found. Commonly used for existence checks.

The method returns the first index at which a given element can be found, or if not present. -- βœ… Implementation How It Works: 1. Negative : If is negative, it is adjusted to count from the end of the array. For example, starts the search from the last element. 2. Array Iteration : The loop starts from the (or if not provided) and compares each element with using strict equality (). 3. Return Index or -1 : If is found, the method returns the index of the first match. If no match is found, it returns . Example Walkthrough: : Starts at index and finds at index , so it returns . : No match is found, so it returns . : Starts at index and finds at index , so it returns . : Starts searching from the last element (), doesn't find , so it returns . : Starts searching from , finds at index , so it returns . This implementation mimics , with additional support for negative , ensuring the search works as expected. -- <!-quiz-start --Q1: What does return? [ ] 0 [x] 1 [ ] 2 [ ] [1, 2] Q2: What comparison does use to find elements? [ ] Loose equality (==) [x] Strict equality (===) [ ] Object.is() [ ] Deep equality Q3: What does return? [ ] 0 [ ] 1 [x] -1 [ ] undefined <!-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
11 of 24
LibraryJavaScriptPolyfills26 of 61

πŸ”’ Array.prototype.indexOf() Polyfill

jspolyfillsmedium

The indexOf() method returns the first index at which a given element can be found, or -1 if not present.


βœ… Implementation

Array.prototype.customIndexOf = function(searchElement, fromIndex = 0) {
    // Normalize negative fromIndex to count from the end of the array
    if (fromIndex < 0) {
        fromIndex = Math.max(this.length + fromIndex, 0); // Adjust negative index
    }

    // Iterate through the array starting from fromIndex
    for (let i = fromIndex; i < this.length; i++) {
        if (this[i] === searchElement) {
            return i; // Return the index of the first match
        }
    }

    return -1; // Return -1 if the element is not found
};

// Example usage:
const array = [2, 9, 9];

console.log(array.customIndexOf(2));       // Output: 0
console.log(array.customIndexOf(7));       // Output: -1
console.log(array.customIndexOf(9, 2));    // Output: 2
console.log(array.customIndexOf(2, -1));   // Output: -1
console.log(array.customIndexOf(2, -3));   // Output: 0

How It Works:

  1. Negative fromIndex : If fromIndex is negative, it is adjusted to count from the end of the array. For example, fromIndex = -1 starts the search from the last element.
  2. Array Iteration : The loop starts from the fromIndex (or 0 if not provided) and compares each element with searchElement using strict equality (===).
  3. Return Index or -1 :
  • If searchElement is found, the method returns the index of the first match.
  • If no match is found, it returns -1.

Example Walkthrough:

  • customIndexOf(2): Starts at index 0 and finds 2 at index 0, so it returns 0.
  • customIndexOf(7): No match is found, so it returns -1.
  • customIndexOf(9, 2): Starts at index 2 and finds 9 at index 2, so it returns 2.
  • customIndexOf(2, -1): Starts searching from the last element (index 2), doesn't find 2, so it returns -1.
  • customIndexOf(2, -3): Starts searching from index 0, finds 2 at index 0, so it returns 0.

This implementation mimics Array.prototype.indexOf, with additional support for negative fromIndex, ensuring the search works as expected.


Quick Quiz

Test your understanding with 3 quick questions

Q1What does `[2, 9, 9].customIndexOf(9)` return?
Q2What comparison does `indexOf()` use to find elements?
Q3What does `[1, 2, 3].customIndexOf(1, 1)` return?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna