Am I allowed to do this in JavaScript?
var 1 = "me";
var 2 = "you";
var 3 = "we";
If not, what's the work around it?
My goal is to create a game where a random number is generated then I answer what that number represents.
Am I allowed to do this in JavaScript?
var 1 = "me";
var 2 = "you";
var 3 = "we";
If not, what's the work around it?
My goal is to create a game where a random number is generated then I answer what that number represents.
Use an object:
var obj = {1: "me",
2: "you",
3: "we"};
Then when you generate the random number you can look up the associated value using obj[number]
. Of course if the numbers are always in sequence you could use an array instead, you would just need a dummy value at the beginning since array indexing starts at 0
, so doing something like var arr = [null, "me", "you", "we"];
would work the same way.
No, you can't. The workaround is to start the variable name with a letter:
var a1 = "me";
var a2 = "you";
var a3 = "we";
you should use array
var answers = ["","me","you","us"];
answers[1] returns me
answers[2] returns you
answers[3] returns us
or use a letter as prefix
like
var b1 = "me";
var b2 = "you";
var b3 = "we";