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
a
b
c
..
..
..
x
y
z
aa
ab
ac
..
..
..
zzx
zzy
zzz
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>
Hello TotallyMe,
Try this :
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>
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(counte r1+65)
if (counter1 != 0 || counter2 != 0 )
value = value + String.fromCharCode(counte r2+65)
//if (counter3 != 0 )
value = value + String.fromCharCode(counte r3+65)
document.writeln(value + "<BR>");
}
}
}
}
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(counte
if (counter1 != 0 || counter2 != 0 )
value = value + String.fromCharCode(counte
//if (counter3 != 0 )
value = value + String.fromCharCode(counte
document.writeln(value + "<BR>");
}
}
}
}
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
generateRange(1); // Generates A-Z
generateRange(2); // Generates A-ZZ
generateRange(3); // Generates A-ZZZ
generateRange(4); // Generates A-ZZZZ
ASKER
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);
}
}
}
}
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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>
You're welcome!
Weird characters ? I don't see them :(
Your code feel good!
Weird characters ? I don't see them :(
Your code feel good!
Thanks for the points!
To get your AA to ZZZ you can nest the loops.
Open in new window