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 Answers8

3

Create an array of those vars and go over like this

for(i = 0; i < 3; i++)
{
    func(arr[i]);
}
Vadiklk
  • 3,696
  • 5
  • 28
  • 44
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.

Tim Hoolihan
  • 12,316
  • 3
  • 41
  • 54
  • 1
    Recommending the usage of eval() to a user new to javascript warrants a down vote imho. – Exelian Feb 17 '11 at 15:12
  • 2
    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. – Tim Hoolihan Feb 17 '11 at 15:17
  • I have recommended the use of the Function constructor. Should be better than eval(). – madr Feb 17 '11 at 15:20
  • 1
    Up-voted because your solution is better than mine; you provided a warning and the proper solution in-respect to the question asked. – Stefano D Feb 17 '11 at 15:28
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>
Stefano D
  • 958
  • 5
  • 17
  • Recommending the usage of eval() to a user new to javascript warrants a down vote imho. – Exelian Feb 17 '11 at 15:11
  • 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. – Stefano D Feb 17 '11 at 15:13
  • Well, you could add a comment about that this is not the preffered way of using eval and warning of it's usage. – Exelian Feb 17 '11 at 15:14
  • @yetanothercoder - I agree too, but the eval statement itself is not doing anything significant. Maybe you meant something like this: `eval('userpos' + i + ' += ' + i);` – erickb Feb 17 '11 at 15:17
  • @stefano: no probs. came across the same the other day :) http://stackoverflow.com/questions/4976090/make-new-javascript-variable-by-combining-string-variable/4976118#4976118 btw you may need to append var to the end. – naveen Feb 17 '11 at 15:19
  • 1
    @exelian: I dont really appreciate you downvoting Tim below as he has provided a warning – naveen Feb 17 '11 at 15:22
1

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

var userpos = new Array(3);

for (var i=0; i<userpos.length; i++) {}
{
    userpos[i] = i;
}
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
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] ); 
Chris
  • 11,780
  • 13
  • 48
  • 70
  • This function is unsave. If you add functions too the prototype of `Array` it'll break unless you add a check for it. – Exelian Feb 17 '11 at 15:12
  • for-in is not recommended. It is greedy when it comes to large collections or arrays. – madr Feb 17 '11 at 15:13
  • Both valid points. My answer was merely to satisfy OP's question. – Chris Feb 17 '11 at 15:31
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;

}

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/

madr
  • 655
  • 4
  • 16
  • i don't see how this code is any safer than eval. either way you're executing source from a string. – Tim Hoolihan Feb 17 '11 at 15:23
  • 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. – madr Feb 17 '11 at 15:34
-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.
}
Exelian
  • 5,749
  • 1
  • 30
  • 49