A Caesar cipher shifts every letter of a message a fixed number of places along the alphabet — shift by 3 and a becomes d, b becomes e — wrapping past z back to a, keeping each letter's case, and leaving spaces, digits, and punctuation untouched. It is one of the oldest known ciphers, named for Julius Caesar, and a common warm-up in coding interviews (background on Wikipedia). You will implement three functions on one object: encode to shift a message, decode to shift it back, and rot13, the shift-by-13 case that is its own inverse.
caesarCipher.encode(text, shift) // shift each letter forward by `shift`, wrapping z -> a
caesarCipher.decode(text, shift) // undo an encode that used the same shift
caesarCipher.rot13(text) // encode with shift 13 — its own inverse
All three live on one object: caesarCipher = { encode, decode, rot13 }. shift is any integer.
caesarCipher.encode('abc', 3); // 'def'
caesarCipher.encode('xyz', 3); // 'abc' — wraps past z
caesarCipher.encode('Hello, World!', 3); // 'Khoor, Zruog!'
caesarCipher.encode('abc', 29); // 'def' — 29 mod 26 is 3
caesarCipher.encode('def', -3); // 'abc' — negative shift
caesarCipher.decode('Khoor, Zruog!', 3); // 'Hello, World!'
caesarCipher.rot13('abc'); // 'nop'
caesarCipher.rot13(caesarCipher.rot13('Secret!')); // 'Secret!' — self-inverse
A-Z stay uppercase and a-z stay lowercase. Anchor each letter to the start of its own case.z comes a again; the shift is taken modulo 26.shift may be 0, larger than 26, or negative; normalize it with modulo 26. Watch that a negative shift % 26 is still negative in JavaScript.decode(encode(text, s), s) returns the original text for every shift s.The full solution is part of Premium
Walkthrough, edge cases, complexity notes, and the runnable editor unlock with a Premium subscription.
Submissions are part of Premium
Unlock community code, comments, reactions, and framework-specific approaches.
A Caesar cipher shifts every letter of a message a fixed number of places along the alphabet — shift by 3 and a becomes d, b becomes e — wrapping past z back to a, keeping each letter's case, and leaving spaces, digits, and punctuation untouched. It is one of the oldest known ciphers, named for Julius Caesar, and a common warm-up in coding interviews (background on Wikipedia). You will implement three functions on one object: encode to shift a message, decode to shift it back, and rot13, the shift-by-13 case that is its own inverse.
caesarCipher.encode(text, shift) // shift each letter forward by `shift`, wrapping z -> a
caesarCipher.decode(text, shift) // undo an encode that used the same shift
caesarCipher.rot13(text) // encode with shift 13 — its own inverse
All three live on one object: caesarCipher = { encode, decode, rot13 }. shift is any integer.
caesarCipher.encode('abc', 3); // 'def'
caesarCipher.encode('xyz', 3); // 'abc' — wraps past z
caesarCipher.encode('Hello, World!', 3); // 'Khoor, Zruog!'
caesarCipher.encode('abc', 29); // 'def' — 29 mod 26 is 3
caesarCipher.encode('def', -3); // 'abc' — negative shift
caesarCipher.decode('Khoor, Zruog!', 3); // 'Hello, World!'
caesarCipher.rot13('abc'); // 'nop'
caesarCipher.rot13(caesarCipher.rot13('Secret!')); // 'Secret!' — self-inverse
A-Z stay uppercase and a-z stay lowercase. Anchor each letter to the start of its own case.z comes a again; the shift is taken modulo 26.shift may be 0, larger than 26, or negative; normalize it with modulo 26. Watch that a negative shift % 26 is still negative in JavaScript.decode(encode(text, s), s) returns the original text for every shift s.The full solution is part of Premium
Walkthrough, edge cases, complexity notes, and the runnable editor unlock with a Premium subscription.
Submissions are part of Premium
Unlock community code, comments, reactions, and framework-specific approaches.
Unlock the solution & editor
Runnable editor + tests
Solve in the browser with instant Jest feedback.
Detailed solutions
Walkthroughs, edge cases, and complexity notes.
Multi-framework variants
React, Vue, Vanilla, Angular — same question, different stacks.