Skip to content

Instantly share code, notes, and snippets.

@mpj
Created January 13, 2020 16:49
Show Gist options
  • Save mpj/cc4005e34f4bffea73d785b8918709ec to your computer and use it in GitHub Desktop.
Save mpj/cc4005e34f4bffea73d785b8918709ec to your computer and use it in GitHub Desktop.
const numbers = [ 2, 4, 8, 9 ]
const addTwo = number => number + 1
const isMoreThan5 = number => number > 5
const result =
//numbers.filter(isMoreThan5)
filterArray(isMoreThan5, numbers)
//mapArray(addTwo, numbers
result
function filterArray(predicate, array) {
let filteredArray = []
for(let i = 0; i < array.length; i++) {
const currentItem = array[i]
if(predicate(currentItem)) {
filteredArray.push(currentItem)
}
}
return filteredArray
}
function mapArray(transform, array) {
let transformedArray = []
for(let i = 0; i < array.length; i++) {
transformedArray.push(transform(array[i]))
}
return transformedArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment