A higher-order function is a function that takes in another function or returns a function. Such functions include map
and filter
.
Custom map
function customMap(values, transform) {
let tranformedArr = [];
for (const value of values) {
const result = transform(value);
tranformedArr.push(result);
}
return tranformedArr;
}
Usage
const numbers = [1, 2, 3, 4, 5, 6];
const squares = customMap(numbers, num => num * num )
console.log(squares); // [ 1, 4, 9, 16, 25, 36 ]