8000 add caesar cipher · schoettker/javascript-exercises@b847c3b · GitHub
[go: up one dir, main page]

Skip to content

Commit b847c3b

Browse files
committed
add caesar cipher
1 parent cadd4b2 commit b847c3b

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ The first exercise, `helloWorld` will walk you through the process in more depth
1313

1414
## planned exercises (in no particular order for the moment):
1515
1. book titles (MC)
16-
1. leap years
17-
1. Caesar Cipher
1816
1. Palindromes
1917
1. Pangrams
2018
1. pig latin (MC)

caesar/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Exercise XX - caesar cipher
2+
3+
Implement the legendary caesar cipher:
4+
5+
> In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.
6+
7+
write a function that takes a string to be encoded and a shift factor and then returns the encoded string:
8+
9+
```javascript
10+
caesar('A', 1) // simply shifts the letter by 1: returns 'B'
11+
```
12+
13+
the cipher should retain capitalization:
14+
```javascript
15+
caesar('Hey', 5) // returns 'Mjd;
16+
```
17+
18+
should _not_ shift punctuation:
19+
```javascript
20+
caesar('Hello, World!', 5) //returns 'Mjqqt, Btwqi!'
21+
```
22+
23+
the shift should wrap around the alphabet:
24+
```javascript
25+
caesar('Z', 1) // returns 'A'
26+
```
27+
28+
negative numbers should work as well:
29+
```javascript
30+
caesar('Mjqqt, Btwqi!', -5) // returns 'Hello, World!'
31+
```
32+
33+

caesar/caesar.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var caesar = function() {
2+
3+
}
4+
5+
module.exports = caesar

caesar/caesar.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var caesar = require('./caesar')
2+
3+
describe('caesar', function() {
4+
it('works with single letters', function() {
5+
expect(caesar('A', 1)).toEqual('B');
6+
});
7+
xit('works with words', function() {
8+
expect(caesar('Aaa', 1)).toEqual('Bbb');
9+
});
10+
xit('works with phrases', function() {
11+
expect(caesar('Hello, World!', 5)).toEqual('Mjqqt, Btwqi!');
12+
});
13+
xit('works with negative shift', function() {
14+
expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!');
15+
});
16+
xit('wraps', function() {
17+
expect(caesar('Z', 1)).toEqual('A');
18+
});
19+
xit('works with large shift factors', function() {
20+
expect(caesar('Hello, World!', 75)).toEqual('Ebiil, Tloia!');
21+
});
22+
});

0 commit comments

Comments
 (0)
0