Hone logo
Hone
Problems

Implementing Array.prototype.lastIndexOf in JavaScript

The lastIndexOf() method is a fundamental array method in JavaScript that finds the last index at which a given element can be found in the array. Implementing this method yourself is a great exercise in understanding array iteration and conditional logic. This challenge asks you to recreate this functionality as an extension to the Array.prototype.

Problem Description

You are tasked with implementing a lastIndexOf method directly on the Array.prototype. This means all arrays in JavaScript will gain this new method. The method should accept two arguments:

  1. searchElement: The element to search for within the array.
  2. fromIndex (optional): The index to start searching backwards from. If omitted, the search starts from the last element of the array.

The method should return the last index at which searchElement is found in the array. If searchElement is not found, the method should return -1. The fromIndex parameter should be handled correctly, including cases where it's out of bounds (less than 0 or greater than or equal to the array's length).

Key Requirements:

  • The implementation must be added to Array.prototype.
  • The method should iterate through the array backwards from the specified fromIndex.
  • The method should return the last index where the element is found.
  • The method should return -1 if the element is not found.
  • The fromIndex parameter should be optional.
  • The fromIndex parameter should be handled correctly for out-of-bounds values.

Examples

Example 1:

Input: [1, 2, 3, 2, 1]
Output: 3
Explanation: The last occurrence of the number 2 is at index 3.

Example 2:

Input: [1, 2, 3, 2, 1], 2
Output: 3
Explanation: The last occurrence of the number 2, starting the search from index 2, is at index 3.

Example 3:

Input: [1, 2, 3, 2, 1], 4
Output: 3
Explanation: The last occurrence of the number 2, starting the search from index 4 (which effectively starts at the end), is at index 3.

Example 4:

Input: [1, 2, 3, 2, 1], -1
Output: 3
Explanation: The last occurrence of the number 2, starting the search from index -1 (which effectively starts at the end), is at index 3.

Example 5:

Input: [1, 2, 3, 2, 1], 5
Output: -1
Explanation: The last occurrence of the number 2, starting the search from index 5 (out of bounds), is not found, so -1 is returned.

Example 6:

Input: [1, 2, 3, 2, 1], 0
Output: 3
Explanation: The last occurrence of the number 2, starting the search from index 0, is at index 3.

Example 7:

Input: [1, 2, 3, 2, 1], -2
Output: 3
Explanation: The last occurrence of the number 2, starting the search from index -2, is at index 3.

Constraints

  • The input array will contain only primitive data types (numbers, strings, booleans).
  • The fromIndex parameter will be an integer.
  • The array length will be between 0 and 1000 (inclusive).
  • The searchElement can be any primitive data type.
  • 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 modifications affect all arrays.
  • Consider how to handle edge cases like an empty array or a fromIndex that is out of bounds.
  • Think about the most efficient way to iterate through the array backwards. Using a for loop is generally a good approach.
  • Be mindful of the behavior when fromIndex is negative. JavaScript's array indexing allows negative indices to count from the end of the array.
Loading editor...
javascript