Skip to content

Instantly share code, notes, and snippets.

@sunnymui
Created March 17, 2022 06:33
Show Gist options
  • Save sunnymui/fbcccde69a68718e88e0148239500dad to your computer and use it in GitHub Desktop.
Save sunnymui/fbcccde69a68718e88e0148239500dad to your computer and use it in GitHub Desktop.
Alphabetic Counter Generator
function* generateAlphabet() {
// generate an increasing counter using letters ie a, b, c, ... z, za, zb, zc, etc
// start at 'A'
let charCode = 65;
let pointer = 0;
let lettersToYield = [];
while (true) {
const letter = String.fromCharCode(charCode);
// assign this letter to the pointer array index
lettersToYield[pointer] = letter;
charCode += 1
// move the pointer right and reset back to 'A'
if (letter === 'Z') {
pointer += 1;
charCode = 65;
}
// yield alphabet incremented string
yield lettersToYield.join('')
}
}
// usage example
// test out our alphabetic count generator fn
const aGen = generateAlphabet();
for (let i = 0; i < 55; i+=1) {
const val = aGen.next().value;
console.log('result', val)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment