| 8 | 8 | * @param {string} [key] |
| 9 | 9 | */ |
| 10 | 10 | 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; | |
| 12 | 42 | } |
| 13 | 43 | |
| 14 | 44 | /** |
| 16 | 46 | * @returns {string} |
| 17 | 47 | */ |
| 18 | 48 | 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 | |
| 20 | 73 | } |
| 21 | 74 | |
| 22 | 75 | /** |
| 24 | 77 | * @returns {string} |
| 25 | 78 | */ |
| 26 | 79 | 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 | |
| 28 | 105 | } |
| 29 | 106 | |
| 30 | 107 | /** |
| 31 | 108 | * @returns {string} |
| 32 | 109 | */ |
| 33 | 110 | get key() { |
| 34 | throw new Error('Remove this line and implement the function'); | |
| 111 | return this._key; | |
| 112 | // Handled Edge Cases: N/A | |
| 35 | 113 | } |
| 36 | 114 | } |