Hone logo
Hone
Problems

Implementing Array.prototype.includes in JavaScript

The Array.prototype.includes() method determines whether an array contains a specific element. This is a fundamental array method used frequently for searching and validation, and understanding its implementation is a great exercise in JavaScript fundamentals. Your task is to implement this method as an extension to the Array prototype.

Problem Description

You need to implement the includes() method directly on the Array.prototype. This method should take two arguments:

  1. searchElement: The value to search for within the array.
  2. fromIndex (optional): The index to start the search from. If omitted, the search starts from the beginning of the array (index 0).

The method should return true if the searchElement is found within the array (starting from fromIndex), and false otherwise. The comparison should use strict equality (===).

Key Requirements:

  • The implementation must be added to Array.prototype.
  • The method should handle the optional fromIndex argument correctly.
  • The method should use strict equality (===) for comparison.
  • The method should return true if the element is found, and false if it is not.
  • The method should handle edge cases gracefully (see below).

Expected Behavior:

  • If fromIndex is positive and within the bounds of the array, the search should begin at that index.
  • If fromIndex is negative, it should be treated as an offset from the end of the array. For example, fromIndex of -1 should start the search from the last element. If fromIndex is less than -array.length, it should be treated as 0.
  • If fromIndex is greater than or equal to the array's length, the search should start from the end of the array.

Edge Cases to Consider:

  • Empty arrays.
  • searchElement being NaN. (Note: NaN === NaN is always false, so includes(NaN) should always return false regardless of the array contents).
  • fromIndex being out of bounds (negative or positive).
  • Arrays containing different data types.

Examples

Example 1:

Input: [1, 2, 3, 4, 5].includes(3)
Output: true
Explanation: The array contains the value 3.

Example 2:

Input: [1, 2, 3, 4, 5].includes(6)
Output: false
Explanation: The array does not contain the value 6.

Example 3:

Input: [1, 2, 3, 4, 5].includes(3, 2)
Output: true
Explanation: The array contains the value 3, starting the search from index 2.

Example 4:

Input: [1, 2, 3, 4, 5].includes(3, -1)
Output: false
Explanation: The array does not contain the value 3, starting the search from the last element (index 4).

Example 5:

Input: [1, 2, NaN].includes(NaN)
Output: false
Explanation: NaN is never strictly equal to itself, so includes(NaN) always returns false.

Constraints

  • The input array can contain any data type.
  • searchElement can be any data type.
  • fromIndex must be an integer.
  • The implementation should be efficient enough to handle arrays of up to 10,000 elements without significant performance degradation.

Notes

Consider using a for loop or for...of loop to iterate through the array. Pay close attention to how you handle the fromIndex parameter, especially negative values and out-of-bounds scenarios. Remember to use strict equality (===) for comparisons. Think about how to handle the edge case of NaN correctly.

Loading editor...
javascript