Click to See Complete Forum and Search --> : Array trouble


travsam
08-21-2005, 11:40 PM
Hello, I am Travsam and I am new in the Forums.

I am trying to develop a code for a game, I want to use two players in the game so I have to alternate them in the program using an array

var cP = new array["1","2"]

then i want to call the variable inside other variables so I can use it to differenciate two variables just by the number assigned in the array.

V.G.

var money1
var money2

is posible to call those variables using a code like this:

var money["cP"]

so i can alternate the two variables during the use of the page?

Greetings

deep.dhyani
08-22-2005, 12:28 AM
Use this code , it will help you

<HTML>
<Head>
</Head>
<Body style="margin:0px">
<script>
a1="TESTING";
c=("a"+"1");
document.write(eval(c));
</script>
</Body>
</HTML>

Best Regards
Deep Dhyani

deep.dhyani
08-22-2005, 12:31 AM
here is another Example

<HTML>
<Head>
</Head>
<Body style="margin:0px">
<script>
arr=["1","2"];
a1="TESTING";
c=("a"+arr[0]);
document.write(eval(c));
</script>
</Body>
</HTML>

BigMoosie
08-22-2005, 12:33 AM
I dont really know what you mean. But you have made a syntax error, to make an array you can do either of the two:

["1","2"]
new Array("1", "2");

But not what you have done.

To create an object like you want you can do this:

function myObject(){
this.cp="Hello";
}

var money=new myObject();

alert(money["cp"]);
alert(money.cp);