How to implement Array.prototype.some? JavaScript Interview Question | Problem Solving | JavaScript Polyfills
In this question, the candidate needs to implement a function customSome
that mimics the behaviour of Array.prototype.some
method.
More about Array.prototype.some
The some()
method tests whether at least one element in the array passes the test implemented by the provided function. It returns true
if, in the array, it finds an element for which the provided function returns true
; otherwise it returns false
. It doesn't modify the array.
Examples
const array = [1, 2, 3, 4, 5];
// checks whether an element is even
const even = (element) => element % 2 === 0;
console.print(array.customSome(even));
// expected output: true
Syntax
The customSome
takes two parameters
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.
thisArg
(Optional)
Object to use as this
inside callbackFn.
customSome(callback, thisArg)
Return value
true
if the callback function returns a truthy value for at least one element in the array. Otherwise, false
.
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