Aysha Azura Aysha Azura - 6 months ago 10
HTML Question

For loops - looping inside loop, for getting the alphabets with their ascii value

I am generating alphabets using their ascii values.
I have got a for loop like this

var alpha = [];
for(var i=65;i<=90;i++){
res = String.fromCharCode(i);
alpha.push(res);
}
document.getElementById("demo").innerHTML = alpha;

for(var i=65;i<=90;i++){
res = String.fromCharCode(i);
alpha.push(res);
}
document.getElementById("demo").innerHTML = alpha;


So when I provide this I am getting an array with result [A.B,C....Z].
Now what I want is after pushing all the 26 alphabets I want [AA,AB,AC...AZ].
How is this possible?

Answer

This might be what you are after:

var alpha = [];

for(var i=65;i<=90;i++){
  alpha.push(String.fromCharCode(i));
}


for(var i=65;i<=90;i++){
  var first = 'A';
  var next = String.fromCharCode(i);
  alpha.push(first + next);
}
document.getElementById('demo').innerHTML = alpha;

You can check out a running example here: https://jsfiddle.net/xr5dwLu1/1/