0

Let's just say that I want variables who's content increase like this: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. But let's just say that I have variables that I want their names to increase like this: aaa, aab, aac, aad, aae, aaf, aag, aah, aai, aaj.

In other words, I want a loop that would create the following:

var aaa = 1;
var aab = 2;
var aac = 3;
var aad = 4;
var aae = 5;
var aaf = 6;
var aag = 7;
var aah = 8;
var aai = 9;
var aaj = 10;

Just saying, the idea in variable name part, is that once it reaches "aaz", it can go on to "aba", and the same when it reaches "azz", it would continue with "zaa".

5
  • 1
    So the javascript program itself will keep declaring variables in a loop? What are you trying to solve? Commented Apr 18, 2015 at 3:15
  • 1
    Have you tried anything? Commented Apr 18, 2015 at 3:15
  • 1
    why do you want that? why not use an array? Commented Apr 18, 2015 at 3:17
  • 1
    I don't see how this would be helpful as it's not like you could easily reference them. Either use an Array like Dyrandz said, or use an Object as a dictionary Commented Apr 18, 2015 at 3:24
  • @DyrandzFamador @vsnyc @JamesWilkins @PaulS. I started using eval(). I could not do much with it though. I do not want to use either arrays or objects, I would like something that can create variables to the var object/array(whatever it is). Commented Apr 18, 2015 at 4:16

2 Answers 2

4

Here's one way you can do it:

var s = ""; 
for (var i = 0, s = ""; i < 26; ++i)
    for (var i2 = 0; i2 < 26; ++i2)
        for (var i3 = 0; i3 < 26; ++i3)
            s += "var " + String.fromCharCode(97+i) + String.fromCharCode(97+i2) + String.fromCharCode(97+i3) + " = " + (i*26*26+i2*26+i3)+";\r\n";

You can create all variables all at once by calling eval(s).

Warning though: Some letter combos are KEYWORDS (as in var new = 8914; is an error), and eval() will fail if you are planning to do it this way. Instead, you should set the values on an object like this:

function createVariables(obj) {
    for (var i = 0, s = ""; i < 26; ++i)
        for (var i2 = 0; i2 < 26; ++i2)
            for (var i3 = 0; i3 < 26; ++i3)
                obj[String.fromCharCode(97+i) + String.fromCharCode(97+i2) + String.fromCharCode(97+i3)] = i*26*26+i2*26+i3;
    return obj;
}

var vars = createVariables({});

You will have a total of 26*26*26, or 17576 variables.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice catch on the reserved words. It would certainly hiccup on var var = ...
This is the closest to what I expected
2

I think it may help you

var cars = ["a", "b", "c", "d"];
var count=1;
var text=new Array() ;
for (var i = 0 ; i < cars.length; i++) {
    for (var j = 0 ; j < cars.length; j++){
        for (var k = 0 ; k < cars.length; k++){
            text.push([cars[i]+cars[j]+cars[k],count]);
            count++;
        }
    }
}
for (var i = 0 ; i < text.length; i++) {
    document.write(text[i][0]+'---'+text[i][1]+'<br>');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.