safe loop counters

Two functions one calling the other and both with for loops with a loop counter i. Declaring the counter variable outside the loop seems to prevent interference.

by Vasil Svetoslavov

HTML

<div id='log'>
</div>

<a href="#" onClick="a(testData);">RUN</a>

JavaScript

var testData = [
{
	name: 'A',
    items: [
    	//"A1", "A2", "A3"
    ]
}, {
	name: 'B',
    items: [
    	//"B1", "B2", "B3"
    ]
}, {
	name: 'C',
    items: [
    	//"C1", "C2", "C3"
    ]
}, {
	name: 'B',
    items: [
    	"B1", "B2", "B3"
    ]
}, {
	name: 'C',
    items: [
    	"C1", "C2", "C3"
    ]
}
];


var a = function (data) {
	
    clear();
    
    var i;
    
	for (i = 0; i < data.length; i++) {
    	append('name: "' + data[i].name + '"');
    	b(data[i]);
    }
    
    append('<hr />');
    append(new Date());
    
};

var b = function(obj) {

	var i;
	for (i = 0; i < obj.items.length; i++) {
    	append('i = "' + i + '", item = "' + obj.items[i]);
    }

};

var clear = function() {
	document.getElementById('log').innerHTML = '';
}

var append = function(txt) {
	document.getElementById('log').innerHTML += '<br />' + txt
};