Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

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

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Chargement dans…3
×

Consultez-les par la suite

1 sur 2 Publicité

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

Télécharger pour lire hors ligne

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]();
}
}
.

Publicité
Publicité

Plus De Contenu Connexe

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

Plus par ellenj4 (20)

Publicité

Plus récents (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](); } }

×