Friday, 9 August 2013

How does this JavaScript code work exactly?

How does this JavaScript code work exactly?

function sum(numbers) {
var total = 0;
forEach(numbers, function (number) {
total += number;
});
return total;
}
show(sum([1, 10, 100]));
p.s forEach is a function that does this:
function forEach(array, action) {
for (var i = 0; i < array.length; i++)
action(array[i]);
}
I would like an explanation of how the code above works, I'm mostly
concerned about how a function can be called as an argument for a
function.

No comments:

Post a Comment