In this article, I will list out some Polyfills asked by companies.
This method is used to flat a nested array.
In the below example, we have used recursion to solve this problem. We have created 2 cases:
- A base case: If depth is reached then push arr in output and return it.
- A recursion case: Loop over array and check if its an Array or not. If its an array flat it, else push the number in output.
This is a Higher Order Function which takes another function and filters the array on the basis of the function.
In the below example, we have created a higher order function, which takes another function and calls on each element of the array. If it returns true then that element is inserted into result.
This is a Higher Order Function which takes another function and reduces the array to single value/object and returns it.
In the below example, we have created a higher order function, which takes another function and calls on each element of the array. It mutates the result returned by the callback function.
The bind method is used to pass an execution context to the function.
In the below example, the custom Bind function takes the context and uses apply method to bind the function with the given context.
For more Awesome polyfills asked in interviews, check out JSVault
Note: I have written all the methods as pure functions, as I have only wanted to show the code. We can also use Prototypal Inheritance.
All the URLs in this post are powered by Sotly.co