• Status: Solved
• Priority: Medium
• Security: Public
• Views: 460

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

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
TotallyMe
• 4
• 4
• 3
• +2
1 Solution

Commented:
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

Commented:
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

PluritechnicianCommented:
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

Commented:
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 Commented:
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 Commented:
Also I'm not sure if it helps but I'm also using jQuery so you can use any of the jQuery functions
0

Commented:
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 Commented:
russellC: Thanks, I've played around with it but it doesn't appear to return any values.
0

Commented:
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

PluritechnicianCommented:
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 Commented:
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

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