I have this function (sample here: http://jsbin.com/emabe4/ )
var a=[];
var log = document.getElementById('log');
function loop(x){
x++;
a.push(x);
if (x < 10){
log.innerHTML = log.innerHTML + "<br/>" + a;
console.log(a);
loop(x)
}
}
As the loop calls itself recursively, the array gets longer and is written out with the inner HTML as such:
[1]
[1,2]
...
[1,2,3,4,5,6,7,8,9]
Which is how I'd like it to work.
But if you look at the console log, this is what you see:
[1,2,3,4,5,6,7,8,9]
[1,2,3,4,5,6,7,8,9]
...
[1,2,3,4,5,6,7,8,9]
The questions:
1) Why the discrepancy between innerHTML and console.log?
2) the console log appears to be what is actually being created and I think that's due to a closure problem, correct? If so, what's the workaround for this to do what I want it to do (the former, where I can interact with the array step-by-step as it grows each time)? I've solved this before in while loops, but not sure how to handle it here.
"" + ait force the generation of a temporary string and it's this string that is displayed by the log, not the array.log.innerHTMLline. And it doesn't work right when you putlog.innerHTML = 'x'. So it seems like it has something to do withinnerHTML?