Generating alphanumerical sequence javascript -


i have written terribly slow function generating codes go aa000 zz999 (in sequence not random). , have concluded there has better way this. suggestions on how make faster?

function generatealphanumeric(){  thealphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']; resultarrray = []; resultarrray2 = []; teller = 0;  for(i in thealphabet){     for(x in thealphabet){         resultarrray[teller] = thealphabet[i] + thealphabet[x];         teller++;     } } teller = 0; for(x = 0; x<10; x++){     for(y = 0; y<10; y++){         for(z = 0; z<10; z++){             resultarrray2[teller] = x.tostring() + y.tostring() +z.tostring();             teller++;         }     } } teller = 0; finalarray = []; for(index in resultarrray){     for(i in resultarrray2){         finalarray[teller] = resultarrray[index] + resultarrray2[i];         teller++;     } } //console.log(resultarrray); //console.log(resultarrray2); console.log(finalarray); } 

this should considerably faster:

var thealphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o', 'p','q','r','s','t','u','v','w','x','y','z']; var thedigits = ['0','1','2','3','4','5','6','7','8','9'];  var result = []; (var i=0 ; i<26 ; i++) {     var prefix1 = thealphabet[i];     (var j=0 ; j<26; j++) {         var prefix2 = prefix1 + thealphabet[j];         for(var x = 0; x<10; x++){             var prefix3 = prefix2 + thedigits[x];             for(var y = 0; y<10; y++){                 var prefix4 = prefix3 + thedigits[y];                 for(var z = 0; z<10; z++){                    result.push(prefix4 + thedigits[z]);                 }             }                }     } } 

key ideas:

  • generate in 1 run
  • reuse partial strings as possible

however, don't see how such exhaustive list useful. there 26 * 26 * 1000 different codes. instead of maintaining array codes make sense build function generates specific code requested:

function getcode(number) {     var z = number % 10;     number -= z; number /= 10;     var y = number % 10;     number -= y; number /= 10;     var x = number % 10;     number -= x; number /= 10;     var = number % 26;     number -= a; number /= 26;     var b = number;      return thealphabet[a] + thealphabet[b] + thedigits[x] + thedigits[y] + thedigits[z]; } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -