-1

I want to combine two array to sum up each element or the array up and return the result array.I AM PRACTICING HIGH ORDER FUNCTION, and although this can be easily solved, I am more inclined to seek advice for higher order function suggestions. something like this:

var a=[1,2,3], b=[7,8,9]; 
func(a,b);// should return [8,10,12];

I need to do it more elegantly using high order functions, so cannot use a a simple loop to add each elements up. I know Map/Reduce function can apply to 1D array, but how about between the arrays? any elegant implementations?

My effort:

var arr=[a,b];
var toReturn=arr[0].map(function(_, i) {
    return arr.reduce(function(max, row) {
      return max+row[i];
    }, 0);
  });

I know it is more complicated than it should be, but i am trying to learn higher order function that's it.

WABBIT0111
  • 2,233
  • 2
  • 18
  • 30

2 Answers2

0

Why are you making this more complicated then it needs to be? The following does exactly what you asked:

function func(a,b) {
    var result = [];
    for(var i=0; i<a.length && i<b.length; i++)
    {
        result[i] = a[i] + b[i];
    }
    return result;
}

You said: "I need to do it more elegantly using high order functions, so cannot use a a simple loop to add each elements up." But you gave no compelling reason and no example that would justify a more complicated solution.

riwalk
  • 14,033
  • 6
  • 51
  • 68
  • var arr=[array1,array2]; var toReturn=arr[0].map(function(_, i) { return arr.reduce(function(max, row) { return max+row[i]; }, 0); }); return toReturn; – WABBIT0111 Feb 09 '15 at 23:14
  • @SevenL, I have no idea what you were trying to say. – riwalk Feb 10 '15 at 05:42
  • sry about last comment. I've updated the question body to include my implementation. Basically i am practicing higher order function using this question as an easier example. Thus the intention is not to solve the problem easily, but to use functions, especially higher order functions. you can take a look at my implementation above. Hope you understand my dilemma as why not just use a loop. – WABBIT0111 Feb 10 '15 at 05:49
0

I think I know what you want, but using high order functions for everything is overkill, in my example too. Here we create map2 that is similar to [].map but expects 2 arrays and a functions that takes at least 2 arguments. I recommend learning a bit more about map reduce because here you don t need reduce at all.

<script>
function sum (a,b) {
    return a + b;
}

function map2 (array1, array2, function1) {
    /*not fully tested*/
    var length1 = array1.length,
        length2 = array2.length,
        i,
        array3 = new Array(length1);
    for (i = 0; i < length1 && i < length2; i++) {
        array3[i] = function1(array1[i], array2[i], i);
    }
    return array3;
}

function sumArrays (array1, array2) {
    return map2(array1, array2, sum);
}
var a=[1,2,3], b=[7,8,9]; 
console.log(sumArrays(a,b));
console.log(sumArrays(a,sumArrays(a,b)))
</script>
Walle Cyril
  • 3,087
  • 4
  • 23
  • 55