Ad
Code
Diff
  • // basic merge callback
    var merge = function(array1, array2, callback){  
    	// error check array lengths with ternary operator
      //(array1.length != array2.length) ? return new Error('array\'s not same length') : null;
     
     	// instantiate our result array that we will return
      var result = [];
     	
      // loop through each index of array1
      array1.forEach( function(v,i) {
     		// append the return of our callback to our results array
        result.push(callback(array1[i], array2[i]));
      });
     
      return result;
    }
    
    // how you would write merge if you wanted to use the map method
    var mergemap = function(array1, array2, callback) {
      return array1.map(function(v, i) {
        return v + array2[i];
      });
    }
    
    // our function call with anonymous callback
    var x = merge([1, 2, 3, 4], [5, 6, 7, 8], function(a, b){  
      return a + b;
    });
    
    console.log('Merge result: ', x);
    //x should now equal [6, 8, 10, 12].
    
    // Think distance formula from physics except
    // it should be able to find distance in any dimensional system
    // 2D = x,y coord, 3D = x,y,z coord, etc.
    var euclid = function(coords1, coords2){  
      //Your code here.
      //You should not use any loops and should
      //instead use your original merge function.
      
      // This does the (x1 - x2 - xN)^2, (y1 + y2 + yN)^2 + ...
      var dimensions = merge(coords1, coords2, function(a, b) {
        return Math.pow((a - b), 2);
      });
      
      // This sums the dimension and takes the square root
      // sqrt( x + y + z + ...)
      // .reduce is acting as a sum function for our dimension array
      // We then use Math.sqrt to get the square root
      // We use Math.round with * 100 and / 100 to round to two decimal places
      // The rounding is optional but makes for a nicer looking result
      var distance = Math.round( Math.sqrt( dimensions.reduce(function(previousVal, currentVal) {
      	return previousVal + currentVal;
    	})) * 100) / 100;
    
    	return distance;
    }
    
    var x = euclid([1.2, 3.67], [2.0, 4.4]);
    console.log('Euclid result: ', x);
    //x should now equal approximately 1.08.
    • // basic merge callback
    • var merge = function(array1, array2, callback){
    • var array3 = [];
    • // error check array lengths with ternary operator
    • //(array1.length != array2.length) ? return new Error('array\'s not same length') : null;
    • // instantiate our result array that we will return
    • var result = [];
    • // loop through each index of array1
    • array1.forEach( function(v,i) {
    • // append the return of our callback to our results array
    • result.push(callback(array1[i], array2[i]));
    • });
    • return result;
    • }
    • 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;
    • // how you would write merge if you wanted to use the map method
    • var mergemap = function(array1, array2, callback) {
    • return array1.map(function(v, i) {
    • return v + array2[i];
    • });
    • }
    • // our function call with anonymous callback
    • var x = merge([1, 2, 3, 4], [5, 6, 7, 8], function(a, b){
    • return a + b;
    • });
    • console.log('Merge result: ', x);
    • //x should now equal [6, 8, 10, 12].
    • // Think distance formula from physics except
    • // it should be able to find distance in any dimensional system
    • // 2D = x,y coord, 3D = x,y,z coord, etc.
    • var euclid = function(coords1, coords2){
    • //Your code here.
    • //You should not use any loops and should
    • //instead use your original merge function.
    • }
    • // This does the (x1 - x2 - xN)^2, (y1 + y2 + yN)^2 + ...
    • var dimensions = merge(coords1, coords2, function(a, b) {
    • return Math.pow((a - b), 2);
    • });
    • // This sums the dimension and takes the square root
    • // sqrt( x + y + z + ...)
    • // .reduce is acting as a sum function for our dimension array
    • // We then use Math.sqrt to get the square root
    • // We use Math.round with * 100 and / 100 to round to two decimal places
    • // The rounding is optional but makes for a nicer looking result
    • var distance = Math.round( Math.sqrt( dimensions.reduce(function(previousVal, currentVal) {
    • return previousVal + currentVal;
    • })) * 100) / 100;
    • var y = euclid([1.2, 3.67], [2.0, 4.4]);
    • return distance;
    • }
    • //y should now equal approximately 1.08.
    • var x = euclid([1.2, 3.67], [2.0, 4.4]);
    • console.log('Euclid result: ', x);
    • //x should now equal approximately 1.08.