I think my understanding of callbacks is pretty green. But this is what have so far and my function is only adding the first index in each array together.
var merge = function(array1, array2, callback){
for (var i = 0; i < array1.length; i++) {
return array1[i] + array2[i];
};
callback(array1[i] + array2[i]);
};
var x = merge([1, 2, 3, 4], [5, 6, 7, 8], function(a, b){
return a + b;
});
console.log(x);
You are returning too early, the first iteration through the loop is going to end the function call.
It looks like you want to use a callback to do the merge work, much like you would with .filter, .sort of Array. If that is the case, you do either the work in the merge function or in the callback not both.
Meaning you either do
array1[i]+array2[i]
in the merge function adding each to a new array, or pass the arguments to the callback and put each result from the callback into the new array.