Finished
44//
55
66export class Crypto {
7 constructor() {
8 throw new Error('Remove this line and implement the function');
7 constructor(plaintext) {
8 this.plaintext = plaintext || '';
9 }
10
11 normalize() {
12 // Edge Case: empty input string
13 if (!this.plaintext) return '';
14
15 return this.plaintext.toLowerCase().replace(/[^a-z0-9]/g, '');
916 }
1017
1118 get ciphertext() {
12 throw new Error('Remove this line and implement the function');
19 const normalized = this.normalize();
20
21 // Edge Case: empty normalized string
22 if (!normalized) return '';
23
24 const length = normalized.length;
25
26 // Calculate rectangle dimensions
27 // Find the smallest c such that r*c >= length, c >= r, and c - r <= 1
28 let c = Math.ceil(Math.sqrt(length));
29 let r = Math.ceil(length / c);
30
31 // Adjust dimensions to meet constraints
32 while (r * c < length) {
33 c++;
34 r = Math.ceil(length / c);
35 }
36
37 // Ensure c >= r and c - r <= 1
38 while (c < r || c - r > 1) {
39 c++;
40 r = Math.ceil(length / c);
41 }
42
43 // Create the rectangle
44 const rectangle = [];
45 for (let i = 0; i < r; i++) {
46 const start = i * c;
47 const end = start + c;
48 let row = normalized.slice(start, end);
49
50 // Pad the row if necessary
51 while (row.length < c) {
52 row += ' ';
53 }
54
55 rectangle.push(row);
56 }
57
58 // Read down the columns
59 let result = '';
60 for (let col = 0; col < c; col++) {
61 for (let row = 0; row < r; row++) {
62 result += rectangle[row][col];
63 }
64
65 // Add space between chunks except for the last one
66 if (col < c - 1) {
67 result += ' ';
68 }
69 }
70
71 return result;
1372 }
1473}
74
75// Handled Edge Cases: empty input string, empty normalized string
Test NameStatus
empty plaintext results in an empty ciphertext
Pass
normalization results in empty plaintext
Pass
Lowercase
Pass
Remove spaces
Pass
Remove punctuation
Pass
9 character plaintext results in 3 chunks of 3 characters
Pass
8 character plaintext results in 3 chunks, the last one with a trailing space
Pass

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