Successfully reported this slideshow.
Your SlideShare is downloading. ×

Please fix bug with a loop and closures- And finally add a line that a.docx

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad

Check these out next

1 of 2 Ad

Please fix bug with a loop and closures- And finally add a line that a.docx

Download to read offline

Please fix bug with a loop and closures. And finally add a line that actually calls testList();
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]();
}
}
Solution
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]();
}
}
.

Please fix bug with a loop and closures. And finally add a line that actually calls testList();
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]();
}
}
Solution
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]();
}
}
.

Advertisement
Advertisement

More Related Content

Similar to Please fix bug with a loop and closures- And finally add a line that a.docx (20)

More from ellenj4 (20)

Advertisement

Recently uploaded (20)

Please fix bug with a loop and closures- And finally add a line that a.docx

  1. 1. Please fix bug with a loop and closures. And finally add a line that actually calls testList(); 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](); } } Solution function buildList(list) {
  2. 2. 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](); } }

×