I need to increment a string from.. let's say aaa
to zzz
and write every incrementation in the console (is incrementation even a word?). It would go something like this:
aaa
aab
aac
...
aaz
aba
abb
abc
...
abz
aca
acb
And so on. So far I have incremented a single letter by doing this:
String.prototype.replaceAt = function(index, character) {
return this.substr(0, index) + character + this.substr(index+character.length);
}
string = "aaa";
string = string.replaceAt(2, String.fromCharCode(string.charCodeAt(2) + 1));
//string == "aab"
However, I am lost when it comes to the final letter being z
and it should then increment letter 2 (index 1) and reset the last letter to be a
.
Does anyone have or know a smart solution to this? Thanks!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With