# Please provide correct answer as well as notes to how it was reached. This one is...

###### Question:

Please provide correct answer as well as notes to how it was reached.

This one is a real gotcha for many people, so you need to understand it. Be very careful if you are defining a function within a loop: the local variables from the closure do not act as you might first think.

The line result.push( function() {alert(item + ' ' + list[i])} adds a reference to an anonymous function three times to the result array. If you are not so familiar with anonymous functions think of it like:

 1 2 pointer = function() {alert(item + ' ' + list[i])}; result.push(pointer);

Note that when you run the example, "item3 undefined" is alerted three times! This is because just like previous examples, there is only one closure for the local variables for buildList. When the anonymous functions are called on the line fnlist[j](); they all use the same single closure, and they use the currentvalue for i and item within that one closure (where i has a value of 3 because the loop had completed, and item has a value of 'item3').

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 function buildList(list) {     var result = [];     for (var i = 0; i < list.length; i++) {         var item = 'item' + list[i];         result.push( function() {alert(item + ' ' + list[i])} );     }     return result; } function testList() {     var fnlist = buildList([1,2,3]);     // using j only to help prevent confusion - could use i     for (var j = 0; j < fnlist.length; j++) {         fnlist[j]();     }

#### Similar Solved Questions

##### X-ray facil 9. During wWI Marie and Irene Curie (her daughter) rigged automoe to be kno...
X-ray facil 9. During wWI Marie and Irene Curie (her daughter) rigged automoe to be kno which could be brought directly to the battlefield. These vehicles c came to be known as " Answer: 10. The discovery of radium was quickly commercialized. In one case a fake person by the name of was created ...
##### Annual Cash Company Dividend per share $18.00 15.00 14.90 Market Value per Share$ 230.77 151.52...
Annual Cash Company Dividend per share $18.00 15.00 14.90 Market Value per Share$ 230.77 151.52 165.56 117.10 3 4 Compute the dividend yield for each of these four separate companies. Ovidend Yield Choose Denominator Company Choose Numerator: Dividend Yield Dividend yield ► 1 . 2. 3 4 1 Which...
##### Find the interval(s) where the function is concave down. 10- 8 6 -10-8-674 LS 16 8...
Find the interval(s) where the function is concave down. 10- 8 6 -10-8-674 LS 16 8 10 O A. (-00. - 1)U(2.0) OB. (-2.3) OC. (-1, 2) OD. (-0. - 2)U(3.00)...
##### Two charged spheres are brought together and separated. What is the final charge for each sphere?
Two charged spheres are brought together and separated. What is the final charge for each sphere?...
##### Can anyone identify this plant for me? I have been looking all over the internet. This...
Can anyone identify this plant for me? I have been looking all over the internet. This plant is very tall, has lots of yellow flowers clustered together in many different regions of the plant. This picture was taken in September of 2016 in Pennsylvania...
##### 250. The school nurse is teaching the faculty is the most effective methods to prevent the...
250. The school nurse is teaching the faculty is the most effective methods to prevent the spread of lice (Pediculus 250. The school nurse is teaching the faculty the most effective methods to prevent the spread of lice (pediculus Humanus Capitis) in the school. The information that would be most im...
##### Problem 5.11 Your brother has asked you for a loan and has promised to pay you...
Problem 5.11 Your brother has asked you for a loan and has promised to pay you \$5,450 at the end of three years. If you normally invest to earn 7.60 percent per year, how much will you be willing to lend to your brother if you view this purely as a financial transaction (i.e., you don't give you...