Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Commit f374ab9

Browse files
UberKlugernowaktz
authored andcommitted
Update 2009-03-06-javascript-best-practices.md
In the "Avoid nested loops" section: Fixed one typo Fixed several semantic errors in the code (see Pull comments)
1 parent 3d19891 commit f374ab9

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/articles/_posts/2009-03-06-javascript-best-practices.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,8 @@ The other problem of nesting is variable names and loops. As you normally start
522522

523523
function renderProfiles(o) {
524524
var out = document.getElementById('profiles');
525+
var ul = document.createElement('ul');
525526
for(var i = 0; i < o.members.length; i++) {
526-
var ul = document.createElement('ul');
527527
var li = document.createElement('li');
528528
li.appendChild(document.createTextNode(o.members[i].name));
529529
var nestedul = document.createElement('ul');
@@ -538,6 +538,7 @@ The other problem of nesting is variable names and loops. As you normally start
538538
nestedul.appendChild(datali);
539539
}
540540
li.appendChild(nestedul);
541+
ul.appendChild(li);
541542
}
542543
out.appendChild(ul);
543544
}
@@ -546,26 +547,27 @@ As I am using the generic — really throw-away — variable names `ul` and `li`
546547

547548
function renderProfiles(o) {
548549
var out = document.getElementById('profiles');
550+
var ul = document.createElement('ul');
549551
for(var i = 0; i < o.members.length; i++) {
550-
var ul = document.createElement('ul');
551552
var li = document.createElement('li');
552-
li.appendChild(document.createTextNode(data.members[i].name));
553-
li.appendChild(addMemberData(o.members[i]));
553+
li.appendChild(document.createTextNode(o.members[i].name));
554+
li.appendChild(addMemberData(o.members[i].data));
555+
ul.appendChild(li);
554556
}
555557
out.appendChild(ul);
556558
}
557-
function addMemberData(member) {
559+
function addMemberData(data) {
558560
var ul = document.createElement('ul');
559-
for(var i = 0; i < member.data.length; i++) {
561+
for(var i = 0; i < data.length; i++) {
560562
var li = document.createElement('li');
561563
li.appendChild(
562564
document.createTextNode(
563-
member.data[i].label + ' ' +
564-
member.data[i].value
565+
data[i].label + ' ' +
566+
data[i].value
565567
)
566568
);
569+
ul.appendChild(li);
567570
}
568-
ul.appendChild(li);
569571
return ul;
570572
}
571573

0 commit comments

Comments
 (0)