I've been struggling with implementing my custom _.each() function within another function and keep encountering the issue of getting "undefined" returned. My goal is to utilize _.each() to apply a test function to an array. Despite being aware that this is likely a simple callback syntax problem, it continues to perplex me.
Any help would be greatly appreciated by this newbie.
Below is the function in question:
_.filter = function(collection, test) {
_.each(collection, test());
};
The result of this is 'undefined'
This is the array I'm passing as 'collection':
[1, 2, 3, 4, 5, 6]
And here's the function I'm passing as 'test':
function (num) { return num % 2 !== 0; }
Here's how my _.each() function looks:
_.each = function(collection, iterator) {
if( Object.prototype.toString.call( collection ) === '[object Array]' ) {
for (var i=0; i<collection.length; i++){
iterator(collection[i], i, collection);
}
} else if (typeof collection === 'object'){
for (var i in collection){
iterator(collection[i], i, collection)
}
} else if (typeof collection === 'int'){
console.log('int')
}
};