Hone logo
Hone
Problems

Implementing Array.prototype.indexOf

The built-in Array.prototype.indexOf() method is a fundamental tool for searching arrays in JavaScript. This challenge asks you to implement this method from scratch, reinforcing your understanding of array iteration and conditional logic. Successfully completing this challenge will give you a deeper appreciation for how core JavaScript functionalities are built.

Problem Description

Your task is to implement a function that extends the Array.prototype with a new method called indexOf. This method should accept two arguments:

  1. searchElement: The value to search for within the array.
  2. fromIndex (optional): The index at which to begin the search. If omitted, the search starts at index 0.

The indexOf method should return the first index at which a given element can be found in the array, or -1 if it is not present. The search should be performed linearly, comparing each element of the array to the searchElement.

Key Requirements:

  • The implementation must be a method of Array.prototype.
  • The function must handle the optional fromIndex argument correctly.
  • The function must return the correct index or -1 as specified.
  • The function should not modify the original array.

Expected Behavior:

  • If the searchElement is found in the array, the method should return the index of its first occurrence.
  • If the searchElement is not found in the array, the method should return -1.
  • If fromIndex is greater than or equal to the array's length, the method should return -1.
  • If fromIndex is negative, it should be treated as if it were 0 (unless it's less than -array.length, in which case it should be treated as -array.length).

Examples

Example 1:

Input: ['a', 'b', 'c', 'd', 'a']
Output: 0
Explanation: The element 'a' is found at index 0.

Example 2:

Input: ['a', 'b', 'c', 'd', 'a']
Output: 4
Explanation: The element 'a' is also found at index 4. The method returns the *first* occurrence.

Example 3:

Input: ['a', 'b', 'c', 'd', 'a']
Output: -1
Explanation: The element 'e' is not present in the array.

Example 4:

Input: ['a', 'b', 'c', 'd', 'a']
Output: -1
Explanation: The search starts at index 3. 'a' is not found at index 3 or later.

Example 5:

Input: ['a', 'b', 'c', 'd', 'a']
Output: 2
Explanation: The search starts at index 2. 'c' is found at index 2.

Example 6: (Edge Case)

Input: ['a', 'b', 'c', 'd', 'a']
Output: -1
Explanation: fromIndex is negative and larger than the array length.

Constraints

  • The input array can contain any type of data (numbers, strings, booleans, objects, etc.).
  • The array length will be between 0 and 1000.
  • The searchElement can be of any data type.
  • The fromIndex will be an integer.
  • Performance: The solution should have a time complexity of O(n) in the worst case, where n is the length of the array.

Notes

  • Remember that Array.prototype methods are inherited by all arrays.
  • Consider how to handle negative fromIndex values correctly.
  • Think about the case where fromIndex is beyond the bounds of the array.
  • You can use a for loop or while loop to iterate through the array.
  • Be mindful of the strict equality operator (===) when comparing elements. It's generally recommended for accurate comparisons.
Loading editor...
javascript