Last active
March 19, 2018 21:02
-
-
Save floh1695/7c99b868a92d51def58c0c684653d54f to your computer and use it in GitHub Desktop.
Re-implementation of Array Higher Order Functions in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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