Created
March 17, 2022 06:33
-
-
Save sunnymui/fbcccde69a68718e88e0148239500dad to your computer and use it in GitHub Desktop.
Alphabetic Counter Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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