0
Hours
0
Minutes
0
Seconds
You need Login/Signup to run/submit the code.

How to implement Array.prototype.every? JavaScript Interview Question | Problem Solving | JavaScript Polyfills

@Yomesh Gupta
1100

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

More about Array.prototype.every

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

Examples

const even = (element) => element % 2 === 0;
const array = [2, 4, 6, 8];

// expected output: true
console.print(array.every(even));

Syntax

The customEvery 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.

const input = [2,4,6,8];
const event = (element) => element % 2 === 0;

// customEvery(callback, thisArg)
customEvery(even, input);

Return Value

true if the callbackFn function returns a truthy value for every array element. 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 15 mins. Share your solution with us -- https://twitter.com/devtoolstech

Loading IDE...