Skip to content

Instantly share code, notes, and snippets.

@floh1695
Last active March 19, 2018 21:02
Show Gist options
  • Save floh1695/7c99b868a92d51def58c0c684653d54f to your computer and use it in GitHub Desktop.
Save floh1695/7c99b868a92d51def58c0c684653d54f to your computer and use it in GitHub Desktop.
Re-implementation of Array Higher Order Functions in JavaScript
const square = (number) => {
return number * number;
}
const double = (number) => {
return number * 2;
}
const map = (array, operation, thisArg = undefined) => {
const newArray = [];
for (let index = 0; index < array.length; index++) {
const element = array[index];
const newElement = operation.call(thisArg, element,
index, array);
newArray.push(newElement);
}
return newArray;
}
const numbers = [1, 2, 3, 4, 5];
const squaredNumbers = map(numbers, square);
const doubledNumbers = map(numbers, double);
console.log('Original: ', numbers);
console.log(' Squared: ', squaredNumbers);
console.log(' Doubled: ', doubledNumbers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment