These two JavaScript functions each accept TWO array arguments and return ONE array result. Conforming to ES3, how can I rewrite these to accept an indefinite number of array inputs?
function sum(v, w) {
for (var a = jsArray(v), b = jsArray(w), t = 0; t < a.length; t++) a[t] += b[t];
return vbArray(a);
}
function mul(v, w) {
for (var a = jsArray(v), b = jsArray(w), t = 0; t < a.length; t++) a[t] *= b[t];
return vbArray(a);
}
The odd jsArray()
function is required because the arrays to be processed are coming from VBA and jsArray() converts them to JavaScript arrays:
function jsArray(v) {
return new VBArray(v).toArray()
}
You can try to use array-like object arguments in order to get all passed arguments:
Example of transformed
sum
function. Beware that this works if all the passed arrays have the same length.Generalized solution:
Solution without
.bind
:Improved version to support ability to implement
avg
: