javascript - How to manipulate arrays with a custom function -
so have array each cell of array containing specific function.
var randomly = [ function(a) { return * - a; }, function(a) { return (a - 2) * (a - 1) * (a - 1); }, function(a) { return * + a; }, function(a) { return % 3; } ];
i'm creating function that takes input value (a) entire array , goes through each array cell individually, solves function. however, result of function becomes next function's input value. initial input value first function, , next function's input falue previous function's result.
to this, created loop...
var thefunction = function (input, queue){ (var = 0; < queue.length; i++){ queue[i](input); }
so, here i'm stuck...
how can call specific array along input value? used queue[i](input);
not sure if that's valid. on appreciated.
you're close, "chain" functions, you'll need store return values, can re-use them:
var thefunction = function (input, queue) { var result = input; (var = 0; < queue.length; i++) result = queue[i](result); return result; }
var randomly = [ function(a) { return * - a; }, function(a) { return (a - 2) * (a - 1) * (a - 1); }, function(a) { return * + a; }, function(a) { return % 3; } ]; var thefunction = function (input, queue) { var result = input; (var = 0; < queue.length; i++) result = queue[i](result); return result; } console.log(thefunction(4, randomly));
Comments
Post a Comment