Forums > Javascript

recalling Array values doesn't work as expected

June 12, 2011 | 12:05 pm

Hi,
i’m only able to recall the values of the Array testi2 when n = 1. Why?
I saved all reopened it but same behavior.

testi1 = new Array("a","b","c")
testi2 = new Array("x","y","z")

function simp(n,s)

{
post (n);
post (s);

if (n=1) {n = testi1;}
if (n=2) {n = testi2;}

post (n[s]);
post ("n");

}

– Pasted Max Patch, click to expand. –

June 12, 2011 | 1:33 pm

You are setting a variable rather than doing a comparison in your if statement. You basically set n to 1 and then immediately set it again to two so and thereby only ever reference the second array. Try changing those two lines so you use == instead of = like so:

if (n==1) {n = testi1;}


June 12, 2011 | 1:33 pm

You are setting a variable rather than doing a comparison in your if statement. You basically set n to 1 and then immediately set it again to two so and thereby only ever reference the second array. Try changing those two lines so you use == instead of = like so:

if (n==1) {n = testi1;}


June 12, 2011 | 3:05 pm

lol…thanks Luke.


June 12, 2011 | 3:05 pm

lol…thanks Luke.


Viewing 5 posts - 1 through 5 (of 5 total)