1

I need to create a function or use if is possible an already made library to auto increment an index. For example if it starts with 'A' it has to be incremented to 'Z' and after 'Z' it has to start from 'A1' and as soon as . . .'B1','C1', ... 'Z1', 'A2','B2',... . Does exist something like this already made ?

My idea is this, but start from 'A' and don't add number . . .

function nextChar(cont,letter) {


if (cont === 0){return letter;}
else {


letter=letter.charCodeAt(0) + 1;

return String.fromCharCode(letter);

   }

}
Francesco1984
  • 501
  • 2
  • 5
  • 9
  • http://stackoverflow.com/questions/12504042/what-is-a-method-that-can-be-used-to-increment-letters – MarsOne Feb 26 '15 at 16:13
  • This is just a nested for-loop, any card game that has to create a deck will do something similar (cards go from 1-13, when 13 is hit, change suit). – Matt K Feb 26 '15 at 16:15

4 Answers4

1

One of many options:

function nextIndex(idx) {
  var m = idx.match(/^([A-Z])(\d*)$/)
  if(!m)
    return 'A';
  if(m[1] == 'Z')
    return 'A' + (Number(m[2] || 0) + 1);
  return String.fromCharCode(m[1].charCodeAt(0) + 1) + m[2];
}

var a = "";
for(i = 0; i < 100; i++) {
  a = nextIndex(a)
  document.write(a + ", ")
}
georg
  • 211,518
  • 52
  • 313
  • 390
0

This one's less efficient than georg's but maybe easier to understand at first glance:

for (var count = 0, countlen = 5; count < countlen; count++) {
    for (var i = 65, l = i + 26; i < l; i++) {
        console.log(String.fromCharCode(i) + (count !== 0 ? count : ''));
    }
}

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
0

Allow me to propose a solution more object-oriented:

function Index(start_with) {
    this.reset = function(reset_to) {
        reset_to = reset_to || 'A';
        this.i = reset_to.length > 1 ? reset_to[1] : 0; // needs more input checking
        this.c = reset_to[0].toUpperCase(); // needs more input checking
        return this;
    };

    this.inc = function(steps) {
        steps = steps || 1;
        while(steps--) {
            if (this.c === 'Z') {
              this.i++;
              this.c = 'A';
            } else {
              this.c = String.fromCharCode(this.c.charCodeAt(0) + 1);
            }
        }
        return this;
    };

    this.toString = function() {
        if (this.i === 0) return this.c;
        return this.c + '' + this.i;
    };

    this.reset(start_with);
}

var a = new Index(); // A
console.log('a = ' + a.inc(24).inc().inc()); // Y, Z, A1
var b = new Index('B8'); // B8
console.log('a = ' + a.reset('Y').inc()); // Y, Z
console.log('b = ' + b); // B8
sebnukem
  • 8,143
  • 6
  • 38
  • 48
0

Another way to think about this is that your "A1" index is just the custom rendering of an integer: 0='A',1='B',26='A1',etc.

So you can also overload the Number object to render your index. The big bonus is that all the math operations still work since your are always dealing with numbers:

Number.prototype.asIndex = function() {
    var n = this;
    var i = Math.floor(n / 26);
    var c = String.fromCharCode('A'.charCodeAt(0) + n % 26);
    return '' + c + (i ? i : '');   
}
Number.parseIndex = function(index) {
    var m;
    if (!index) return 0;
    m = index.toUpperCase().match(/^([A-Z])(\d*)$/);
    if (!m || !m[1]) return 0;
    return Number((m[1].charCodeAt(0) - 'A'.charCodeAt(0)) + 26 * (m[2] ? m[2] : 0));
};

var c = 52;
var ic = c.asIndex();
var nc = Number.parseIndex(ic);
console.log(c+' = '+ic+' = '+nc); // 52 = A2 = 52

If you go this way I would try to check if the new methods don't already exist first...

sebnukem
  • 8,143
  • 6
  • 38
  • 48