I'm learning javascript. I know we can pass a function to other functions after the function is defined. But I need help on understanding this example:
function map(func, array) {
var result = [];
forEach(array, function (element) {
result.push(func(element));
});
return result;
}
From what I can understand, func is an argument of map. I need to provide a function as func. But in the tutorial I'm reading, it doesn't mention where this func come from, seems no need to specify this argument? Another example in the tutorial is the same:
function count(test, array) {
return reduce(function(total, element) {
return total + (test(element) ? 1 : 0);
}, 0, array);
}
This test function is equal to element === 0 ? 1 : 0 , but the tutorial doesn't say I need to write down the test function. Do I need to write this test function?
map. That function isMath.round. You can optionally create your own function to pass instead.funcfunction yourself; the example usesMath.roundas the function, so the example callsMath.roundon each number in the array, and returns an array of the input numbers, rounded usingMath.round.