Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to increment a set of 2 letters using GAS?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading