| 3 | 3 | // convenience to get you started writing code faster. |
| 4 | 4 | // |
| 5 | 5 | |
| 6 | | export const encode = () => { |
| 7 | | throw new Error('Remove this line and implement the function'); |
| 6 | export const encode = (plaintext) => { |
| 7 | // Edge Case: Handle null or undefined input |
| 8 | if (plaintext === null || plaintext === undefined) { |
| 9 | return ''; |
| 10 | } |
| 11 | |
| 12 | // Convert to lowercase and remove non-alphanumeric characters |
| 13 | let cleaned = plaintext.toLowerCase().replace(/[^a-z0-9]/g, ''); |
| 14 | |
| 15 | // Edge Case: Handle empty string after cleaning |
| 16 | if (cleaned.length === 0) { |
| 17 | return ''; |
| 18 | } |
| 19 | |
| 20 | let result = ''; |
| 21 | |
| 22 | for (let i = 0; i < cleaned.length; i++) { |
| 23 | const char = cleaned[i]; |
| 24 | |
| 25 | // If it's a number, keep it as is |
| 26 | if (char >= '0' && char <= '9') { |
| 27 | result += char; |
| 28 | } |
| 29 | // If it's a letter, apply Atbash substitution |
| 30 | else { |
| 31 | const code = char.charCodeAt(0) - 'a'.charCodeAt(0); |
| 32 | const substituted = String.fromCharCode('z'.charCodeAt(0) - code); |
| 33 | result += substituted; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // Group into chunks of 5 characters |
| 38 | let grouped = ''; |
| 39 | for (let i = 0; i < result.length; i++) { |
| 40 | if (i > 0 && i % 5 === 0) { |
| 41 | grouped += ' '; |
| 42 | } |
| 43 | grouped += result[i]; |
| 44 | } |
| 45 | |
| 46 | return grouped; |
| 47 | // Handled Edge Cases: null/undefined input, empty string after cleaning, strings with only numbers, |
| 48 | // strings with special characters, mixed case input, grouping boundary edge cases |
| 8 | 49 | }; |
| 9 | 50 | |
| 10 | | export const decode = () => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 51 | export const decode = (ciphertext) => { |
| 52 | // Edge Case: Handle null or undefined input |
| 53 | if (ciphertext === null || ciphertext === undefined) { |
| 54 | return ''; |
| 55 | } |
| 56 | |
| 57 | // Remove spaces and non-alphanumeric characters |
| 58 | let cleaned = ciphertext.toLowerCase().replace(/[^a-z0-9]/g, ''); |
| 59 | |
| 60 | // Edge Case: Handle empty string after cleaning |
| 61 | if (cleaned.length === 0) { |
| 62 | return ''; |
| 63 | } |
| 64 | |
| 65 | let result = ''; |
| 66 | |
| 67 | for (let i = 0; i < cleaned.length; i++) { |
| 68 | const char = cleaned[i]; |
| 69 | |
| 70 | // If it's a number, keep it as is |
| 71 | if (char >= '0' && char <= '9') { |
| 72 | result += char; |
| 73 | } |
| 74 | // If it's a letter, apply Atbash substitution |
| 75 | else { |
| 76 | const code = char.charCodeAt(0) - 'a'.charCodeAt(0); |
| 77 | const substituted = String.fromCharCode('z'.charCodeAt(0) - code); |
| 78 | result += substituted; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return result; |
| 83 | // Handled Edge Cases: null/undefined input, empty string after cleaning, strings with only numbers, |
| 84 | // strings with spaces, mixed case input |
| 12 | 85 | }; |