I need to develop a custom higher order filter() function in order to filter out words with less than 6 letters from an array of 10 words. However, my current implementation is only returning true and false instead of actually filtering out the words that do not meet the criteria. How can I modify my code to achieve the desired outcome?
It's important to note that I am not permitted to use the built-in filter function for this task.
const wordArray = ["apple", "banana", "mango", "kiwi", "orange", "pear", "grape", "melon", "papaya", "guava"];
//myFilterFunction(HOF) takes an array (arr) and custom function (fn) as parameters//
let myFilterFunction = (arr) => (fn) => {
const filteredArray = [];
for (let i = 0; i < arr.length; i++) {
if(fn(arr[i])) {
filteredArray.push(arr[i]);
}
}
return filteredArray; //return a new filtered array
};
//Utilize myFilterFunction with an anonymous function to check each word length//
const result = myFilterFunction(wordArray)((word) => word.length >= 6);
//Display the filtered array on the console//
console.log("Filtered array only containing words with 6 or more letters: " + result);