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 s
I took a different approach with this, using a permutations function which recursively generated all the possible permutations one could generate using characters from an array repeated n times. The code looks like this.
//recursively generates permutations
var permutations = function (li, rep) {
var i, j, next, ret = [];
// base cases
if (rep === 1) {
return li;
}
if (rep <= 0) {
return [];
}
// non-base case
for (i = 0; i < li.length; i += 1) {
// generate the next deepest permutation and add
// the possible beginnings to those
next = permutations(li, rep-1);
for (j = 0; j < next.length; j += 1) {
ret.push(li[i] + next[j]);
}
}
return ret;
};
// returns an array of numbers from [start, end)
// range(10, 14) -> [10, 11, 12, 13]
var range = function (start, end) {
var i, ret = [];
for (i = start; i < end; i+= 1) {
ret.push(i);
}
return ret;
};
// generates letters ('abcd...')
var letters = String.fromCharCode.apply(this, range('a'.charCodeAt(0), 'z'.charCodeAt(0)+1));
// calls the function itself, and .join's it into a string
document.body.innerHTML = (permutations(letters, 3)).join(' ');