Finished
33// convenience to get you started writing code faster.
44//
55
6export const encode = () => {
7 throw new Error('Remove this line and implement the function');
6export const encode = (message, rails) => {
7 // Edge Case: Handle null or undefined message
8 if (message === null || message === undefined) {
9 return '';
10 }
11
12 // Edge Case: Handle zero or negative rails
13 if (rails <= 0) {
14 return '';
15 }
16
17 // Edge Case: Handle single rail
18 if (rails === 1) {
19 return message;
20 }
21
22 // Edge Case: Handle empty message
23 if (message.length === 0) {
24 return '';
25 }
26
27 // Create an array for each rail
28 const fence = Array(rails).fill().map(() => []);
29
30 let currentRail = 0;
31 let direction = 1;
32
33 // Distribute characters across rails in a zigzag pattern
34 for (let i = 0; i < message.length; i++) {
35 fence[currentRail].push(message[i]);
36
37 // Change direction when reaching top or bottom rail
38 if (currentRail === 0) {
39 direction = 1;
40 } else if (currentRail === rails - 1) {
41 direction = -1;
42 }
43
44 currentRail += direction;
45 }
46
47 // Concatenate all rails to form the encoded message
48 return fence.map(rail => rail.join('')).join('');
849};
950
10export const decode = () => {
11 throw new Error('Remove this line and implement the function');
51export const decode = (encodedMessage, rails) => {
52 // Edge Case: Handle null or undefined encoded message
53 if (encodedMessage === null || encodedMessage === undefined) {
54 return '';
55 }
56
57 // Edge Case: Handle zero or negative rails
58 if (rails <= 0) {
59 return '';
60 }
61
62 // Edge Case: Handle single rail
63 if (rails === 1) {
64 return encodedMessage;
65 }
66
67 // Edge Case: Handle empty encoded message
68 if (encodedMessage.length === 0) {
69 return '';
70 }
71
72 // Create a fence to map the zigzag pattern
73 const fence = Array(rails).fill().map(() => Array(encodedMessage.length).fill(null));
74
75 let currentRail = 0;
76 let direction = 1;
77
78 // Mark the positions in the zigzag pattern
79 for (let i = 0; i < encodedMessage.length; i++) {
80 fence[currentRail][i] = true;
81
82 if (currentRail === 0) {
83 direction = 1;
84 } else if (currentRail === rails - 1) {
85 direction = -1;
86 }
87
88 currentRail += direction;
89 }
90
91 // Fill the fence with characters from the encoded message
92 let charIndex = 0;
93 const result = Array(rails).fill().map(() => []);
94
95 for (let r = 0; r < rails; r++) {
96 for (let c = 0; c < encodedMessage.length; c++) {
97 if (fence[r][c] === true) {
98 result[r][c] = encodedMessage[charIndex];
99 charIndex++;
100 }
101 }
102 }
103
104 // Read the message following the zigzag pattern
105 let decoded = '';
106 currentRail = 0;
107 direction = 1;
108
109 for (let i = 0; i < encodedMessage.length; i++) {
110 decoded += result[currentRail][i];
111
112 if (currentRail === 0) {
113 direction = 1;
114 } else if (currentRail === rails - 1) {
115 direction = -1;
116 }
117
118 currentRail += direction;
119 }
120
121 return decoded;
12122};
123
124// Handled Edge Cases: null/undefined message, zero/negative rails, single rail, empty message
Test NameStatus
encode with two rails
Pass
encode with three rails
Pass
encode with ending in the middle
Pass
decode with three rails
Pass
decode with five rails
Pass
decode with six rails
Pass

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