🔢 Array.prototype.findIndex() Polyfill
Category: js / polyfills
Difficulty: medium
The findIndex() method returns the index of the first element that satisfies the provided testing function, or -1 if none is found. 💡 How It Works Iterate through the array: Loop over each element in the array. Apply the callback function: For each element, invoke the provided callback function. Return the index: If the callback returns a truthy value for the current element, return its index. Return -1: If no element matches the condition, return -1. Implementation: [code example] Explanation of How It Works: Iteration over the array: The method iterates through each element of the array using a for loop. The loop will go from index 0 to this.length - 1. Executing the callback function: For each element, the provided callbackFn is executed with three arguments: this[i] (the current element), i (the index of the element), and this (the array itself). The callback function should return a truthy value for the matching element. If it does, the method immediately returns the index i. Return -1 if no match is found: