adom adom - 1 year ago 137
Javascript Question

Increment Alphabet in JavaScript

I want to show alphabet like this in Javascript;


I tried like this;

var cols[A, B, C, D...Z];


for (var i = cols[0]; i < cols.length; i++) {
for(var j = cols[0]; j < cols.length; j++) {
var k = 'i' + 'j';

document.write(<span> + cols.join + </span>);

Could you tell me how to write source code?

Answer Source

Yes, this is pretty easy to accomplish with 2 arrays, with one containing an extra blank entry

var char1 = ['A','B','C','D','E','F']; // shortened for demo - make a..z
var char2 = ['','A','B','C','D','E','F']; // shortened for demo, make a..z

var result = [];
for(var i=0;i<char2.length;i++)
   for(var j=0;j<char1.length;j++)
      result.push(char2[i] + char1[j])

for(var x=0;x<result.length;x++)
    document.write(result[x] + " ")

As was noted in comments, making an array of characters is much easier in practice using

var char1 = 'ABCDEF'.split('');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download