I will assume that the function you have only does ROT13. If it was just +1 to the offset of the letter, you could just use a for loop, where each time you take your previous output and pass it through again and again.
Here's the shortest and most elegant way I could think of to code this:
var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('')
function nextLetter(letter) {
var index = alphabet.indexOf(letter)
return alphabet[(index+1) % 26]
}
function caesarShiftBy1(text) {
return text.split('').map(nextLetter).join('')
}
function allCaesarShifts(text) {
var temp = text.toLowerCase();
for (var i=0; i<26; i++) {
console.log(temp);
temp = caesarShiftBy1(temp);
}
}
Resulting in:
allCaesarShifts('abcdefghijklmnopqrstuvwxyz')
abcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyza
cdefghijklmnopqrstuvwxyzab
defghijklmnopqrstuvwxyzabc
efghijklmnopqrstuvwxyzabcd
fghijklmnopqrstuvwxyzabcde
ghijklmnopqrstuvwxyzabcdef
hijklmnopqrstuvwxyzabcdefg
ijklmnopqrstuvwxyzabcdefgh
jklmnopqrstuvwxyzabcdefghi
klmnopqrstuvwxyzabcdefghij
lmnopqrstuvwxyzabcdefghijk
mnopqrstuvwxyzabcdefghijkl
nopqrstuvwxyzabcdefghijklm
opqrstuvwxyzabcdefghijklmn
pqrstuvwxyzabcdefghijklmno
qrstuvwxyzabcdefghijklmnop
rstuvwxyzabcdefghijklmnopq
stuvwxyzabcdefghijklmnopqr
tuvwxyzabcdefghijklmnopqrs
uvwxyzabcdefghijklmnopqrst
vwxyzabcdefghijklmnopqrstu
wxyzabcdefghijklmnopqrstuv
xyzabcdefghijklmnopqrstuvw
yzabcdefghijklmnopqrstuvwx
zabcdefghijklmnopqrstuvwxy
edit: now recursive by request:
function allCaesarShifts(text) {
var toReturn = [];
function helper(text, offset) {
toReturn +=[ caesarShift(text,offset) ];
if (offset>0)
helper(text, offset-1);
}
helper(text, 26);
return toReturn;
}
More elegant would be to make a function shiftLetter(letter,offset=1), caesarShiftBy(text,offset=1), and then map a curried version of caesarShifyBy(text=text,N) over the range 1,2,...26 (but javascript without jquery doesn't have nice primitives for this stuff yet).