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

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
Asked:
TotallyMe
  • 4
  • 4
  • 3
  • +2
1 Solution
 
russellCCommented:
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++)
{
     alert(String.fromCharCode(i));
}

Open in new window

0
 
infochandruCommented:
Hope this helps you
<html>
<head>
<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>
</head>
<body>
<input type="button" value="Print"
onclick="print()" />

</body>
</html>

Open in new window

0
 
leakim971PluritechnicianCommented:
Hello TotallyMe,

Try this :

<html>
<head>
<script>
	function generateThree() {
		for(a=96;a<123;a++) {
			for(b=96;b<123;b++) {
				for(c=97;c<123;c++) {
						alert(String.fromCharCode(c) + String.fromCharCode((b==96)?32:b) + String.fromCharCode((a==96)?32:a));
				}
			}
		}
		
	}
</script>
<body onload="generateThree()">
</body>
</html>

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Gurvinder Pal SinghCommented:
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
 
TotallyMeAuthor 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
 
TotallyMeAuthor 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
 
russellCCommented:
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);
            }        
        }
    }  
}
  

Open in new window

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


<html>
<head>
<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);
			}
			if(r%27!=0) alert((w + String.fromCharCode(96+r%27)));
		}
	}
</script>
<body>
<input type="button" onClick="generateRange(2);" value="click here">
</body>
</html>

Open in new window

0
 
TotallyMeAuthor 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>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<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>
</head>

<body onLoad="createRange(3)">
  <div id="result"></div>
</body></html>

Open in new window

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

Your code feel good!
0
 
leakim971PluritechnicianCommented:
Thanks for the points!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now