Solved

# How do I generate a range of letters A-ZZZ with Javascript?

Posted on 2010-01-05
307 Views
I need help creating a a loop that generates a range of letters. For example I could specify A to ZZZ and it would generate :

a
b
c
..
..
..
x
y
z
aa
ab
ac
..
..
..
zzx
zzy
zzz
0
Question by:TotallyMe

LVL 3

Expert Comment

This should get you started.  65 - 90 = ASCII "A - Z"

To get your AA to ZZZ you can nest the loops.
``````for (var i =65; i < 91; i++)

{

}
``````
0

LVL 3

Expert Comment

Hope this helps you
``````<html>
<script type="text/javascript">
function print()
{
var arr = new Array("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");

for(i=0; i<arr.length;i++)
{
document.write(arr[i]+"<br>");
}
for(i=0; i<arr.length;i++)
{
for(j=0;j<arr.length;j++)
{
document.write(arr[i]+arr[j]+"<br>");
}
}
for(i=0; i<arr.length;i++)
{
for(j=0;j<arr.length;j++)
{
for(k=0;k<arr.length;k++)
{
document.write(arr[i]+arr[j]+arr[k]+"<br>");
}
}
}
}
</script>
<body>
<input type="button" value="Print"
onclick="print()" />

</body>
</html>
``````
0

LVL 81

Expert Comment

Hello TotallyMe,

Try this :

``````<html>
<script>
function generateThree() {
for(a=96;a<123;a++) {
for(b=96;b<123;b++) {
for(c=97;c<123;c++) {
}
}
}

}
</script>
</body>
</html>
``````
0

LVL 40

Expert Comment

try this

function createChars()
{
for ( var counter1 = 0; counter1 < 26; counter1++)
{
for ( var counter2 = 0; counter2 < 26; counter2++)
{
for ( var counter3 = 0; counter3 < 26; counter3++)
{
var value= ""
if (counter1 != 0 )
value = String.fromCharCode(counter1+65)
if (counter1 != 0 || counter2 != 0 )
value = value + String.fromCharCode(counter2+65)
//if (counter3 != 0 )
value = value + String.fromCharCode(counter3+65)
document.writeln(value + "<BR>");
}
}
}
}

0

Author Comment

leakim971 and gurvinder372 I really like your methods, I'm just wondering would it be possible so that when I call the function I could specify how many characters to generate. For example :

generateRange(1); // Generates A-Z
generateRange(2); // Generates A-ZZ
generateRange(3); // Generates A-ZZZ
generateRange(4); // Generates A-ZZZZ
0

Author Comment

Also I'm not sure if it helps but I'm also using jQuery so you can use any of the jQuery functions
0

LVL 3

Expert Comment

Try this recursion function
``````// JavaScript Document

function createChars(range)

{

var value = null;

var rangeCounter = 0;

createList(value);

function createList(value)

{

for ( var counter1 = 0; counter1 < 26; counter1++)

{

value = value + String.fromCharCode(counter1+65);

if (rangeCounter < range)

{

rangeCounter++:

value = value + createList(value);

}

}

}

}

``````
0

Author Comment

russellC: Thanks, I've played around with it but it doesn't appear to return any values.
0

LVL 3

Expert Comment

were not quite there yet but we are getting close.  I am finally at home now let me take a look at it for a bit and Ill send you a working function now that I can test it myself
0

LVL 81

Accepted Solution

Work for me :

``````<html>
<script>
function generateRange(q) {
str = "";
for(r=1;r<=Math.pow(26,q)+(26*(q-1));r++) {
w = "";
for(p=Math.pow(26,q-1);p>1;p/=26) {
s = parseInt(r/(p+1));
if(s>0) w += String.fromCharCode(96+s);
}
}
}
</script>
<body>
</body>
</html>
``````
0

Author Comment

Thanks leakim971 had a look at that but noticed it was throwing some weird characters. I've been playing around and I think I've solved it, this is what I've come up with :

Let me know if it could be done better.

``````<html>

<script>

function get(x,app){

var x1 = '';

var c

for(c=97;c<123;c++) {

if(x-1>0){

x1 += get(x-1,String.fromCharCode(c)+app)

}else{

x1 +=String.fromCharCode(c)+app

x1 +='<br>'

}

}

return x1

}

function createRange(num) {

for(a=1;a<=num;a++) {

\$('#result').append(get(a,''))

}

}

</script>

<div id="result"></div>

</body></html>
``````
0

LVL 81

Expert Comment

You're welcome!
Weird characters ? I don't see them :(

0

LVL 81

Expert Comment

Thanks for the points!
0

## Featured Post

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…