How could this one be tweaked so that it could increment a set of two letters, so that it'd look like this:
AA, AB, AC...AZ, BA, BB, BC, etc
This is borrowed from tckmn, but it addresses one letter only.
var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
function incrementChar(c) {
var index = alphabet.indexOf(c)
if (index == -1) return -1 // or whatever error value you want
return alphabet[index + 1 % alphabet.length]
}
Appreciate your help!
You just need two loops. One to iterate over the alphabet, and the second to iterate over the alphabet on each iteration of the first loop.
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const arr = [];
for (let i = 0; i < alphabet.length; i++) {
for (let j = 0; j < alphabet.length; j++) {
arr.push(`${alphabet[i]}${alphabet[j]}`);
}
}
console.log(arr);
In your situation, how about the following sample script?
const increase = s => {
const idx = [...s].reduce((c, e, i, a) => (c += (e.charCodeAt(0) - 64) * Math.pow(26, a.length - i - 1)), -1);
// Ref: https://stackoverflow.com/a/53678158
columnIndexToLetter = (n) => (a = Math.floor(n / 26)) >= 0 ? columnIndexToLetter(a - 1) + String.fromCharCode(65 + (n % 26)) : "";
return columnIndexToLetter(idx + 1);
};
const samples = ["A", "Z", "AA", "AZ", "ZZ"];
const res1 = samples.map(e => increase(e));
console.log(res1); // <--- [ 'B', 'AA', 'AB', 'BA', 'AAA' ]
// If you want to give one value, please use the following script.
const sample = "AA";
const res2 = increase(sample);
console.log(res2); // <--- AB
["A", "Z", "AA", "AZ", "ZZ"] is converted to [ 'B', 'AA', 'AB', 'BA', 'AAA' ].