6 Replies Latest reply on Aug 9, 2006 7:17 PM by Craig Grummitt

# better way

which is a better way (smaller) to write this:

if (p1 == 1) {res1 = "RIGHT";}
else {res1 = "WRONG";}

if (p2 == 1) {res2 = "RIGHT";}
else {res2 = "WRONG";}
...

(this will repeat till p10==1)

thanks
• ###### 1. Re: better way

"Caurj" <webforumsuser@macromedia.com> wrote in message news:ebdajp\$595\$1@forums.macromedia.com...
> which is a better way (smaller) to write this:
>
> if (p1 == 1) {res1 = "RIGHT";}
> else {res1 = "WRONG";}
>
> if (p2 == 1) {res2 = "RIGHT";}
> else {res2 = "WRONG";}
> ...
>
> (this will repeat till p10==1)
>
> thanks
>

for (var a = 1; a<= 10; a ++)
{
var r = eval("res" + a); // get a pointer to the res1 to res10 variable
var p = eval("p" + a); // get a pointer to the p1 to p10 variable

if ( p == 1 )
r = "RIGHT";
else
r = "WRONG";
}

That otta do it.
tralfaz

• ###### 2. Re: better way
correct me if i'm wrong tralfaz, but i believe you're copying the contents of res1 to res10 into r, rather than a pointer. (same with p)

Caurj - apologies if i'm telling you about something you already know about, but you may want to have a look at Boolean variables.
Try:

for (var a = 1; a<=10; a++) {
if (this["p"+a] == 1) {
this["res"+a]= "RIGHT";
} else {
this["res"+a]= "WRONG";
}
}
• ###### 3. Re: better way
"Craig Grummitt" <webforumsuser@macromedia.com> wrote in message news:ebdsft\$q08\$1@forums.macromedia.com...
> correct me if i'm wrong tralfaz, but i believe you're copying the contents of
> res1 to res10 into r, rather than a pointer. (same with p)
>
> but you may want to have a look at Boolean variables.
> Try:
>
> for (var a = 1; a<=10; a++) {
> if (this["p"+a] == 1) {
> this["res"+a]= "RIGHT";
> } else {
> this["res"+a]= "WRONG";
> }
> }

Both eval and this[ ] return pointers to objects. There isn't a bit of difference in the results. It's easy to test it. (why
didn't you?)
tralfaz

• ###### 4. Re: better way
> Both eval and this[ ] return pointers to objects. There isn't a bit of difference in the results. It's easy to test it. (why
> didn't you?)
> tralfaz

Craig.. I'm sorry. I guess I made some changes to the code after I tested it trying to simplify things. I checked it again and I
see that you are right. I've often used the eval() function to return pointers to objects but it's not working for variables. Your
code is better. Thanks for pointing out my error.
tralfaz

• ###### 5. Re: better way
tralfaz i think you've misunderstood my point. yes i agree that in essence eval and this will do the same thing here - that's just a personal preference to use 'this'. it's also true that they can return pointers to objects. however caurj is referring to variables, not objects, in which case they will return the contents of the variable.

to illustrate:
a="hello"
var b=eval("a"); // or var b=this["a"]
b="goodbye";
trace(a); //outputs "hello"

feel free to test it.
• ###### 6. Re: better way
stop press - just caught your last post... ;) no worries i probably could have explained more thoroughly.