Implementing a custom filter function in JavaScript

Day 6 of #30DaysOfJavaScript on LeetCode Today's challenge: 2634 — Filter Elements from Array The task was to implement a custom filter function that returns a new array containing only the elements that satisfy a given condition — similar to the built-in Array.filter() method, but without using it. Here’s my solution 👇 var filter = function(arr, fn) { let ans = []; for (let i = 0; i < arr.length; i++) { if (fn(arr[i], i)) ans.push(arr[i]); } return ans; }; This challenge was a great way to understand how callback functions and conditional logic work together in JavaScript — it really helps appreciate how built-in methods like filter() operate under the hood! If you’d like to try it out, check it here: https://lnkd.in/g6WC5mu7 #JavaScript #LeetCode #CodingChallenge #LearningJourney #30DaysOfCode #Functions #Callbacks #Arrays #Programming #Filter

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories