JS Built-ins
FunctionalProgrammingbeginner

Custom map β€” higher-order function

πŸ“– Usage Example
function myMap(arr, fn) {
  const result = [];
  for (const item of arr) result.push(fn(item));
  return result;
}
myMap([1, 2, 3], x => x * 2); // [2, 4, 6]

A higher-order function takes another function as an argument to customize behavior.

Higher-Order Functions

Higher-order functions accept other functions as arguments or return functions.

Challenge: Implement myMap that takes an array and a function, applies the function to each element, and returns a new array with the results.

πŸ’‘ Hints (3)
  • β€’ Use reduce to build new array
  • β€’ Spread operator can add elements to accumulator
  • β€’ fn is applied to each element