8000 caesar · davidngu3/javascript-exercises@85d228f · GitHub
[go: up one dir, main page]

Skip to content

Commit 85d228f

Browse files
committed
caesar
1 parent af50bcc commit 85d228f

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

caesar/caesar.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1-
const caesar = function() {
2-
3-
}
4-
5-
module.exports = caesar
1+
const caesar = function(string, shift) {
2+
return string
3+
.split("")
4+
.map(char => shiftChar(char, shift))
5+
.join("");
6+
};
7+
8+
const codeSet = code => (code < 97 ? 65 : 97);
9+
10+
const mod = (n, m) => (n % m + m) % m;
11+
12+
const shiftChar = (char, shift) => {
13+
const code = char.charCodeAt();
14+
15+
if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
16+
return String.fromCharCode(
17+
mod(code + shift - codeSet(code), 26) + codeSet(code)
18+
);
19+
}
20+
return char;
21+
};
22+
23+
module.exports = caesar;

caesar/caesar.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ describe('caesar', function() {
44
it('works with single letters', function() {
55
expect(caesar('A', 1)).toEqual('B');
66
});
7-
xit('works with words', function() {
7+
it('works with words', function() {
88
expect(caesar('Aaa', 1)).toEqual('Bbb');
99
});
10-
xit('works with phrases', function() {
10+
it('works with phrases', function() {
1111
expect(caesar('Hello, World!', 5)).toEqual('Mjqqt, Btwqi!');
1212
});
13-
xit('works with negative shift', function() {
13+
it('works with negative shift', function() {
1414
expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!');
1515
});
16-
xit('wraps', function() {
16+
it('wraps', function() {
1717
expect(caesar('Z', 1)).toEqual('A');
1818
});
19-
xit('works with large shift factors', function() {
19+
it('works with large shift factors', function() {
2020
expect(caesar('Hello, World!', 75)).toEqual('Ebiil, Tloia!');
2121
});
22-
xit('works with large negative shift factors', function() {
22+
it('works with large negative shift factors', function() {
2323
expect(caesar('Hello, World!', -29)).toEqual('Ebiil, Tloia!');
2424
});
2525
});

0 commit comments

Comments
 (0)
0