I am trying to get the "merge function" to work by writing a callback.

I think my understanding of callbacks is pretty green. But this is what so far, and my function only adds the first index to 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);

      

+3


source to share


3 answers


You come back too early, the first iteration through the loop ends up calling the function.

It sounds like you want to use a callback for the merge to work, just like you did with . filter , . sort of the array. If so, you are either doing the work in the merge function or not both in the callback.

Assuming you are either doing array1[i]+array2[i]

in the merge function by adding each to a new array, or by passing arguments to the callback and placing each result from the callback into a new array.



var merge = function(array1, array2, callback){  
  //test to make sure arrays are same length
  if(array1.length != array2.length){
     throw new Error('Arrays are of different lengths'); 
  }
  var out = [];
  for (var i = 0; i < array1.length; i++) {
    if(callback){
      out.push( callback(array1[i],array2[i]) );
    } else {
      out.push( array1[i] + array2[i] );
    }
  }
  return out;
};

var x = merge([1, 2, 3, 4], [5, 6, 7, 8], function(a, b){  
  return a + b;
});
var x2 = merge([1, 2, 3, 4], [5, 6, 7, 8]);

document.body.innerHTML = x.toString();
document.body.innerHTML += '<br>'+x2.toString();

//will cause the error to be thrown
var x2 = merge([1, 2, 3, 4], [6, 7, 8]);
      

Run codeHide result


+1


source


When you use a callback, the flow of your code is now not returned via callbacks and is assigned to a variable. I see a mixture of usage in your code and it is not clear to me what your code is trying to do. return

inside the for loop ends the merge function before calling the callback.

I could adapt your code to this:

var merge = function(array1, array2, callback){  
  var result = [];
  for (var i = 0; i < array1.length; i++) {
   result[i] = array1[i] + array2[i];
  };
  callback(result);
};

var x = merge([1, 2, 3, 4], [5, 6, 7, 8], function(merged) {
    console.log(merged);
});

      



This will write this result to the console:

[6, 8, 10, 12]

      

To go further, I would also use Math.max(array1.length, array2.length)

in a loop for

to ensure that the result array is as long as the longest input array. There are a bit more complications in there, but I would like to point out a potential problem with this.

+2


source


Your return statement is the reason why your function returns the sum of only the first indices of each array. It completes the function call and never reaches your callbacks. This should help you get it working. Note. There are thousands of ways to accomplish what you are trying to do here.

var merge = function(array1, array2, callback){ 
  var sum=0; 
  for (var i = 0; i < array1.length; i++) { 
    sum = sum + callback(array1[i],array2[i]); 
  } 
  return sum;
}; 
var x = merge([1, 2, 3, 4], [5, 6, 7, 8], 
           function(a, b){ return a + b; }); 
console.log(x);

      

+1


source







All Articles