Taken from Execute Program
Write a function that takes an array and a callback function, then returns the number of elements where callback returns
true
. Use.forEach
to iterate through the array.
Solution
This is the answer I came up with:
// typescript
// Function type (takes in param of any type, returns any type)
type Callback = (param: any) => any
function count(arr: Array<any>, callback: Callback) {
let count = 0
arr.forEach(element => {
if (callback(element)) {
count += 1
}
})
return count
}