I'm having problems with the next code:
test1 = [0,0];
function func(test)
{
var test2 = new Array();
for(var i = 0; i < test.length; i++)
if(test[i] == 0)
{
test[i] = 1;
test2.push(test);
test[i] = 0;
}
return test2;
}
a = func(test1);
document.write(a[0].toString()+"<br/>");
document.write(a[1].toString());
and the output is:
0,0
0,0
I have already checked with the console: when I change test[i] inside the condition, after test2.push(test), test2[test2.length] is also changed. (WHY?)
Is there a scope problem? What should I do to get an output like this?
1,0
0,1
Thanks.
a = func(test); //what is test here??
– linuxeasy Mar 7 '12 at 6:55