Skip to main content
Participant
August 9, 2006
Question

better way

  • August 9, 2006
  • 6 replies
  • 329 views
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
This topic has been closed for replies.

6 replies

Craig Grummitt
Inspiring
August 10, 2006
stop press - just caught your last post... ;) no worries i probably could have explained more thoroughly.
Craig Grummitt
Inspiring
August 10, 2006
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.
Inspiring
August 10, 2006
> 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


Inspiring
August 10, 2006
"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)
>
> 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";
> }
> }

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 Grummitt
Inspiring
August 9, 2006
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";
}
}
Inspiring
August 9, 2006

"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