Topic Description

Given an integer array arr and a filter function fn, return a filtered array filteredArr. The function fn accepts one or two parameters:

The filteredArr should only include elements from arr where the expression fn(arr[i], i) evaluates to a truthy value. A truthy value is a value where Boolean(value) returns true. Do not use the built-in JavaScript Array.filter method to solve this problem.

// Example 1
Input: arr = [0, 10, 20, 30], fn = function greaterThan10(n) { return n > 10; }
Output: [20, 30]
Explanation:
const newArray = filter(arr, fn); // [20, 30]
The function filters out numbers that are not greater than 10.

// Example 2
Input: arr = [1, 2, 3], fn = function firstIndex(n, i) { return i === 0; }
Output: [1]
Explanation:
fn can also take the index of each element. In this case, the function filters out numbers where the index is not 0.

Answer

function filter(arr, fn){
	const resultArr = []
	for(let i=0; i<arr.length; i++){
		const item = arr[i]
		if( fn(item,i) ){
			resultArr.push(item)
		}
	}
	
	return resultArr
}