spending more and more time studying JS and I'm loving it. However, as I begin to deal with slightly more complex functions (IIFE, anonymous, etc) - I'm beginning to struggle and would love some pointers. Would appreciate all the help and advice in explaining why my line of code (Line 15) is causing so much grief!
The assignment instructions are below:
- Build a function and assign it to a variable named applyAndEmpty.
- The function should take in an input number and a queue of functions as parameters.
- Using a for loop, call the functions in the queue in order with the input number, where the results of each function become the next function’s input. Additionally, the queue should be empty after the function is called.
- Lastly, call the applyAndEmpty function using the provided start variable and the puzzlers Queue as arguments, and alert the result.
So far, I know I have correctly setup the for loop and am on the right track as far as calling the actual function, but I simply can't figure out the final line within the loop. I realize I need to use the array shift method in order to sequentially begin emptying out the array, however, I am not sure how I can apply the input parameter to each function, store that as input and then continue to cycle through as I remove the functions, before returning a result.
// The following array and 'start' variable are given (by the assignment):
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; }
];
var start = 2;
// My code begins below:
var applyAndEmpty = function(input,queue) {
for(var i = 0; i < queue.length; i++) {
input = queue[i].shift(); // This is my problem area - I simply don't understand!
}
return input;
};
alert(applyAndEmpty(start,puzzlers));