Ad
Code
Diff
  • var merge = function(array1, array2, callbackFN){ 
     return callbackFN(array1,array2);
    }
    
    function callbackFN(a,b) {
      return a.map(function(c,i){return c + b[i];});
    }
    
    var x = merge([1, 2, 3, 4], [5, 6, 7, 8], callbackFN);
    console.log(x);   //x should now equal [6, 8, 10, 12].
    
    • var merge = function(array1, array2, callback){
    • var array3 = [];
    • if (array1.length !== array2.length) {
    • console.log("Array length mismatch");
    • return new Error("Array length mismatch");
    • } else {
    • length = array1.length;
    • }
    • for (var i = 0; i < length; i++) {
    • array3[i] = callback(array1[i], array2[i]);
    • }
    • return array3;
    • var merge = function(array1, array2, callbackFN){
    • return callbackFN(array1,array2);
    • }
    • var x = merge([1, 2, 3, 4], [5, 6, 7, 8], function(a, b){
    • return a + b;
    • });
    • //x should now equal [6, 8, 10, 12].
    • var euclid = function(coords1, coords2){
    • //Your code here.
    • //You should not use any loops and should
    • //instead use your original merge function.
    • function callbackFN(a,b) {
    • return a.map(function(c,i){return c + b[i];});
    • }
    • var y = euclid([1.2, 3.67], [2.0, 4.4]);
    • //y should now equal approximately 1.08.
    • var x = merge([1, 2, 3, 4], [5, 6, 7, 8], callbackFN);
    • console.log(x); //x should now equal [6, 8, 10, 12].