🔢 Array.prototype.indexOf() Polyfill
Category: js / polyfills
Difficulty: medium
The indexOf() method returns the first index at which a given element can be found, or -1 if not present. ✅ Implementation [code example] How It Works: 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. Array Iteration : The loop starts from the fromIndex (or 0 if not provided) and compares each element with searchElement using strict equality (===). 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....