How to implement Array.prototype.includes? JavaScript Interview Question | Problem Solving | JavaScript Polyfills
In this question, the candidate needs to implement a function customIncludes
that mimics the behaviour of Array.prototype.includes
method.
More about Array.prototype.includes
The includes()
method determines whether an array includes a certain value among its entries, returning true
or false
as appropriate.
Examples
const array = [5, 12, 8, 130, 44];
const found = array.customIncludes(5);
// prints true
console.print(found);
Syntax
The customIncludes
takes two parameters
searchElement
The value to search for.
fromIndex
(Optional)
The position in this array at which to begin searching for searchElement.
The first element to be searched is found at fromIndex
for positive values of fromIndex
, or at arr.length + fromIndex
for negative values of fromIndex
.
Defaults to 0.
Return value
A boolean value which is true if the value searchElement is found within the array (or the part of the array indicated by the index fromIndex, if specified).
array.customIncludes(searchElement);
array.customIncludes(searchElement, fromIndex);
Submission
Start the timer, complete your solution, and test it against the test cases provided by the platform. Ideally, you should finish this question within 30 mins. Share your solution with us -- https://twitter.com/devtoolstech