Finished
88 * @param {string} [key]
99 */
1010 constructor(key) {
11 throw new Error('Remove this line and implement the function');
11 // Edge Case: No key provided - generate random key with at least 100 lowercase letters
12 if (key === undefined) {
13 this._key = this._generateRandomKey(100);
14 return;
15 }
16
17 // Edge Case: Empty key provided
18 if (key === '') {
19 throw new Error('Key must not be empty');
20 }
21
22 // Edge Case: Key contains non-lowercase letters
23 if (!/^[a-z]+$/.test(key)) {
24 throw new Error('Key must contain only lowercase letters');
25 }
26
27 this._key = key;
28 }
29
30 /**
31 * Generate a random key consisting of lowercase letters
32 * @param {number} length
33 * @returns {string}
34 */
35 _generateRandomKey(length) {
36 let result = '';
37 const letters = 'abcdefghijklmnopqrstuvwxyz';
38 for (let i = 0; i < length; i++) {
39 result += letters.charAt(Math.floor(Math.random() * letters.length));
40 }
41 return result;
1242 }
1343
1444 /**
1646 * @returns {string}
1747 */
1848 encode(inText) {
19 throw new Error('Remove this line and implement the function');
49 // Edge Case: Empty plaintext
50 if (inText === '') {
51 return '';
52 }
53
54 let result = '';
55 for (let i = 0; i < inText.length; i++) {
56 const char = inText[i];
57 const keyChar = this._key[i % this._key.length];
58
59 // Edge Case: Non-alphabetic characters in plaintext (preserved as-is)
60 if (!/[a-z]/.test(char)) {
61 result += char;
62 continue;
63 }
64
65 const charCode = char.charCodeAt(0) - 97; // a=0, b=1, ..., z=25
66 const keyCode = keyChar.charCodeAt(0) - 97;
67 const newCharCode = (charCode + keyCode) % 26;
68 result += String.fromCharCode(newCharCode + 97);
69 }
70
71 return result;
72 // Handled Edge Cases: empty plaintext, non-alphabetic characters in plaintext
2073 }
2174
2275 /**
2477 * @returns {string}
2578 */
2679 decode(inText) {
27 throw new Error('Remove this line and implement the function');
80 // Edge Case: Empty ciphertext
81 if (inText === '') {
82 return '';
83 }
84
85 let result = '';
86 for (let i = 0; i < inText.length; i++) {
87 const char = inText[i];
88 const keyChar = this._key[i % this._key.length];
89
90 // Edge Case: Non-alphabetic characters in ciphertext (preserved as-is)
91 if (!/[a-z]/.test(char)) {
92 result += char;
93 continue;
94 }
95
96 const charCode = char.charCodeAt(0) - 97;
97 const keyCode = keyChar.charCodeAt(0) - 97;
98 // Add 26 before modulo to handle negative results
99 const newCharCode = (charCode - keyCode + 26) % 26;
100 result += String.fromCharCode(newCharCode + 97);
101 }
102
103 return result;
104 // Handled Edge Cases: empty ciphertext, non-alphabetic characters in ciphertext
28105 }
29106
30107 /**
31108 * @returns {string}
32109 */
33110 get key() {
34 throw new Error('Remove this line and implement the function');
111 return this._key;
112 // Handled Edge Cases: N/A
35113 }
36114}
Test NameStatus
can encode
Pass
can decode
Pass
is reversible
Pass
key is made only of lowercase letters
Pass
can encode
Pass
can decode
Pass
is reversible
Pass
can double shift encode
Pass
can wrap on encode
Pass
can wrap on decode
Pass
can encode messages longer than the key
Pass
can decode messages longer than the key
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.