Code School Part 3 Exercise 1.13

This one stumped me for a bit. In scrum a fellow teammate said he’d offer some suggestions if I sent him my code.

This was my code prior to suggestions from teammate:

var applyAndEmpty = function(input, queue){
var amount = queue.length;
for (var i=0;i<amount.length;i++){
input = queue(input);
var removeFirst = queue.shift();
}
};

return applyAndEmpty(start, puzzlers);

This is my final code that worked in the exercise:

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;
var applyAndEmpty = function(input, queue){
var amount = queue.length;
for (var i=0;i<amount;i++){
input = queue.shift()(input);
}
return input;
};
alert(applyAndEmpty(start, puzzlers));

My teammate pointed out that I was calling .length twice. With the below statement, I was so close! They were not the right syntax and I was writing more complex statements than they had to be.

input = queue(input);
var removeFirst = queue.shift();

My code also had the applyAndEmpty as a return statement and it should have been an alert statement.

Puzzle Exercise Instructions

Now the people at Poplar Puzzles would like you to treat an array of functions like a Queue, passing the result of each function into the next until the Queue is empty. They’ve sent you the puzzlers Queue of functions, and the following instructions:

In a variable called applyAndEmpty, build and store a function that takes in an input number and a Queue of functions as parameters. Using a for loop, apply the Queue’s functions in order to 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. – PuZzLe MaSTeRs

Lastly, call the applyAndEmpty function using the provided start variable and the puzzlers Queue as arguments, and alert the result.

Error Message with incorrect code

Incorrect Submission: Make sure that you declare the applyAndEmpty variable properly.

 

More of my CodeSchool JavaScript Road Trip journey can be found on GitHub.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s