How to implement Array.prototype.findIndex? JavaScript Interview Question | Problem Solving | JavaScript Polyfills
@Yomesh Gupta

JavaScriptBeginner
217
Do you see value in our efforts?

Devtools Tech is a free platform for Frontend Engineers. It takes a lot of time, effort, and finanical resources to keep the platform running. If you see any value in our work and would like to support us then you can do using the following options.

InternationalDomestic

In this question, the candidate needs to implement a function customFindIndex that mimics the behaviour of Array.prototype.findIndex method.

More about Array.prototype.findIndex

The findIndex() method returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.

Examples

const array = [1,2,3,12,13,15];

const greaterThanTen = (element) => element > 10;

console.print(array.findIndex(greaterThanTen));
// expected output: 3

Syntax

The customFindIndex takes two parameters

  1. callback function

Function to execute on each value in the array. The function is called with the following arguments:

element

  • The current element in the array.

index

  • The index (position) of the current element in the array.

array

  • The array that customFind was called on.

The callback must return a truthy value to indicate a matching element has been found.

  1. thisArg (Optional)

Object to use as this inside callbackFn.

customFindIndex(callback, thisArg)

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 15 mins. Share your solution with us -- https://twitter.com/devtoolstech

Loading IDE...