I have two functions, they do look alike but what I don't really understand is when inside the for
-loop, since the input is an array, why doesn't the array need any index to call the first array?
I have an array of...
var puzzlers = [
function(a) { return 8 * a - 10; },
function(a) { return (a - 3) * (a - 3) * (a - 3); },
function(a) { return a * a + 4; },
function(a) { return a % 5; }
];
I'm trying to loop through the array with an input. The result of the first function will then be used as the next function's input then the first array will be removed.
This is what I wrote...
function applyAndEmpty(input, queue)
{
var length = queue.length;
for(var i = 0; i < length; i++)
{
input = queue[0](input);
queue.shift();
}
return input;
}
The above does give me the answer but then I see that there's another way of writing it which is
var applyAndEmpty = function(input, queue)
{
var length = queue.length;
for(var i = 0; i < length; i++)
{
input = queue.shift()(input);
}
return input;
};
What I don't understand is the part input = queue.shift()(input)
.
Doesn't the queue
need an index?