2

say I wanted to use variables like

var userpos1 : int; var userpos2 : int; var userpos3 : int; //in a for loop like var i=1; for (i=1;i<=3;i++) { userposi + 1 } 

how would I place the i so that the for loop goes through all of my variables properly

var userpos1var : int; var userpos2var : int; var userpos3var : int; //in a for loop like var i=1; for (i=1;i<=3;i++) { userposivar + 1 } 

is there something I need to do to this i to make it work properly such as a "" or a [] around it?

8 Answers 8

3

Create an array of those vars and go over like this

for(i = 0; i < 3; i++) { func(arr[i]); } 
Sign up to request clarification or add additional context in comments.

Comments

2

you should use an array of variables instead, but to do what you are wanting to do, you would write:

eval("userpos" + i + "var") + 1 

eval can be unsafe to use, and does not peform well.

4 Comments

Recommending the usage of eval() to a user new to javascript warrants a down vote imho.
Exelian - read my answer. it has not been edited. I recommended and array instead, and warned about it being unsafe and slow. But I'm not going to ignore the fact that eval does EXACTLY what he was asking for this case. As an adult, I assume the user can take the info, the warning, and make a decision.
I have recommended the use of the Function constructor. Should be better than eval().
Up-voted because your solution is better than mine; you provided a warning and the proper solution in-respect to the question asked.
2
<script type="text/javascript"> var userpos1 = 1; var userpos2 = 2; var userpos3 = 3; var i = 1; for (i=1;i<=3;i++) { alert (eval("userpos" + i)); } </script> 

6 Comments

Recommending the usage of eval() to a user new to javascript warrants a down vote imho.
Your opinion is wrong, I'm simply providing a solution to his question. Would I do it, no, but that's how in JS you can solve the problem he presented.
Well, you could add a comment about that this is not the preffered way of using eval and warning of it's usage.
@yetanothercoder - I agree too, but the eval statement itself is not doing anything significant. Maybe you meant something like this: eval('userpos' + i + ' += ' + i);
@exelian: I dont really appreciate you downvoting Tim below as he has provided a warning
|
1

Why don't you use an array... ?

var userpos = new Array(3); for (var i=0; i<userpos.length; i++) {} { userpos[i] = i; } 

Comments

0

This is much easier done by storing those values in a single array and iterating over the array.

someArray = new Array(1, 2, 3); for (key in someArray) alert(someArray[key] ); 

3 Comments

This function is unsave. If you add functions too the prototype of Array it'll break unless you add a check for it.
for-in is not recommended. It is greedy when it comes to large collections or arrays.
Both valid points. My answer was merely to satisfy OP's question.
0

Instead of doing it this way, use an array of user positions:

//Create an array of 3 positions, setting all to 0

var userPos=new Array(0, 0, 0);

//loop through each position - notice starts at 0.

for (var i = 0; i < 2; i++)

{

userPos[i] += 1;

}

Comments

0

Eval() would do it, but we should not encourage the use of it. Instead, construct an anonymous function.

for ( i = 1; i <= 3; i++ ) { alert(new Function('return userpos' + i + 'var;')()); // value of userpos1var } 

Update JSFiddle example: http://jsfiddle.net/madr/AHBrd/

2 Comments

i don't see how this code is any safer than eval. either way you're executing source from a string.
If I remember correctly, the Function constructor is a small performance boost (I have no source for that though), plus it's a bit more elegant IMHO.
-1

What you're looking for is:

var userpos = ['a','b','c']; for(var i=0; i < userpos.length; i++) { userpos[i]; // doing something with it. } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.