6

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?

4
  • Maybe it is w3schools. Commented Jun 16, 2012 at 2:06
  • eloquentjavascript.net/chapter6.html#p31aabc64 Commented Jun 16, 2012 at 2:11
  • @Jenny: Updated my answer. In that tutorial, a pre-defined function is being passed to map. That function is Math.round. You can optionally create your own function to pass instead. Commented Jun 16, 2012 at 2:15
  • 1
    @Jenny In that link you don't need to write the func function yourself; the example uses Math.round as the function, so the example calls Math.round on each number in the array, and returns an array of the input numbers, rounded using Math.round. Commented Jun 16, 2012 at 2:15

2 Answers 2

4

EDIT: In the link to the tutorial you posted, the function passed is a pre-defined function Math.round. My example below shows creating your own function to pass.


The map example shows the implementation. You'd provide the function (and the Array) when you call map.

From the looks of it, the map pass the current item in the Array to your function, and your function should do something with it and return the result. The results your function returns are added to the new Array.

var arr = [1,2,3,4,5];
var my_func = function(item) { return item * 2; };

var new_arr = map(my_func, arr);

console.log(new_arr); // [2,4,6,8,10]
  1. we created an Array (arr),

  2. we created a function (my_func), which takes whatever it's given, and multiplies it by 2.

  3. we passed both to map

  4. the map function iterates our arr, passing the current item in each iteration to our function.

  5. our function takes the current item, and returns the result of multiplying it by 2.

  6. the map function takes that result, and adds it to the new Array.

  7. when the iteration is done, the map function returns the new Array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for helping. After reading your answer, I just figured out the equals is the function test.
0

When you call map, you pass an inline function, or an existing function.

// The following would return [2,3,4]
map(function(item) { return item + 1}, [1,2,3]);

function double(item) {return item*2}
map(double, [1,2,3])
// returns [2,4,6]

// Your example, Math.round
map(Math.round, [0.01, 2, 9.89, Math.PI])
// returns [0,2,10,3]

Another way to say it is, a function can be passed to another function just like any other argument. This is on the same lines that a function is just like any other variable can be passed or returned from a function

function createAdder(toAdd) {
    return function (num) {
        return num + toAdd;
    }
}

var add5 = createAdder(5);
add5(2); // returns 7


var add7 = createAdder(7);
add7(-2); // returns 5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.