最终实现效果应该如下
aaa aab aac
...
zzx zzy zzz;
实现方法一:
function n2s (n) {
var s = '';
while (s.length < 3) {
s = String.fromCharCode(97 + n % 26) + s;
n = Math.floor(n / 26);
}
return s;
}
var result = [];
var zzz = Math.pow(26, 3) - 1;
for (var n = 0; n <= zzz; n++) {
result.push(n2s(n));
}
document.body.innerHTML = result.join(' ');
实现方法二:
function generate(str)
{
var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
var chars = [];
for(var i = 0; i < str.length; i++)
{
chars.push(alphabet.indexOf(str[i]));
}
for(var i = chars.length - 1; i >= 0 ; i--)
{
var tmp = chars[i];
if(tmp >= 0 && tmp < 25) {
chars[i]++;
break;
}
else{chars[i] = 0;}
}
var newstr = '';
for(var i = 0; i < chars.length; i++)
{
newstr += alphabet[chars[i]];
}
return newstr;
}
function loop(init){
var temp = init;
var str = "";
while(true)
{
temp = generate(temp);
if(temp == init) break;
str += temp + " ";
}
return str;
}
document.body.innerHTML = loop("aaa");