Link to home
Start Free TrialLog in
Avatar of TotallyMe
TotallyMe

asked on

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
Avatar of russellC
russellC
Flag of United States of America image

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

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

Avatar of leakim971
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

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>");
                              }
                        }
                  }
            }

Avatar of TotallyMe
TotallyMe

ASKER

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

russellC: Thanks, I've played around with it but it doesn't appear to return any values.
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
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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

Your code feel good!
Thanks for the points!