8000 Finish caesar · iamjethrooo/javascript-exercises@22c4a86 · GitHub
[go: up one dir, main page]

Skip to content

Commit 22c4a86

Browse files
committed
Finish caesar
1 parent 311e60b commit 22c4a86

File tree

5 files changed

+85
-14
lines changed

5 files changed

+85
-14
lines changed
File renamed without changes.
File renamed without changes.

DONE/caesar/caesar.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const caesar = function(plaintext, shift) {
2+
const ALPHABET = [
3+
"a",
4+
"b",
5+
"c",
6+
"d",
7+
"e",
8+
"f",
9+
"g",
10+
"h",
11+
"i",
12+
"j",
13+
"k",
14+
"l",
15+
"m",
16+
"n",
17+
"o",
18+
"p",
19+
"q",
20+
"r",
21+
"s",
22+
"t",
23+
"u",
24+
"v",
25+
"w",
26+
"x",
27+
"y",
28+
"z",
29+
];
30+
// Convert plaintext to array of characters to compare them to alphabet array
31+
let temp = plaintext.split('');
32+
plaintext = temp;
33+
let ciphertext = "";
34+
if (shift < 0) {
35+
shift = ALPHABET.length - Math.abs(shift);
36+
}
37+
38+
// Removes unnecesssary wrap cycles
39+
while (shift > 26) {
40+
shift -= 26;
41+
}
42+
43+
for (let i = 0; i < plaintext.length; i++) {
44+
let c = plaintext[i];
45+
let needsWrap = (ALPHABET.indexOf(c.toLowerCase()) + shift + 1) > ALPHABET.length;
46+
let isNotLetter = c.toLowerCase() == c.toUpperCase() || c.trim() == "";
47+
let isLowercase = ALPHABET.indexOf(c) != -1;
48+
49+
// If character is not a letter, just add to ciphertext
50+
if (isNotLetter) {
51+
ciphertext += c;
52+
continue;
53+
}
54+
55+
// If character is lowercase
56+
if (isLowercase) {
57+
if (needsWrap) {
58+
ciphertext += ALPHABET[ALPHABET.indexOf(c) + shift - ALPHABET.length];
59+
continue;
60+
}
61+
ciphertext += ALPHABET[ALPHABET.indexOf(c) + shift];
62+
continue;
63+
}
64+
// If character is uppercase
65+
else {
66+
c = c.toLowerCase();
67+
if (needsWrap) {
68+
ciphertext += ALPHABET[ALPHABET.indexOf(c) + shift - ALPHABET.length].toUpperCase();
69+
continue;
70+
}
71+
ciphertext += ALPHABET[ALPHABET.indexOf(c.toLowerCase()) + shift].toUpperCase();
72+
continue;
73+
}
74+
75+
}
76+
return ciphertext;
77+
}
78+
79+
module.exports = caesar

caesar/caesar.spec.js renamed to DONE/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
});

caesar/caesar.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0