| 8 | 8 | * @param {number} rails |
| 9 | 9 | * @returns {string} |
| 10 | 10 | */ |
| 11 | | export const encode = () => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 11 | export const encode = (msg, rails) => { |
| 12 | // Edge Case: Handle null or undefined message |
| 13 | if (msg === null || msg === undefined) { |
| 14 | return ''; |
| 15 | } |
| 16 | |
| 17 | // Edge Case: Handle zero or negative rails |
| 18 | if (rails <= 0) { |
| 19 | return ''; |
| 20 | } |
| 21 | |
| 22 | // Remove spaces for the cipher (as shown in the example) |
| 23 | const cleanMsg = msg.replace(/\s/g, ''); |
| 24 | |
| 25 | // Edge Case: Handle single rail |
| 26 | if (rails === 1) { |
| 27 | return cleanMsg; |
| 28 | } |
| 29 | |
| 30 | // Edge Case: Handle more rails than message length |
| 31 | if (rails > cleanMsg.length) { |
| 32 | return cleanMsg; |
| 33 | } |
| 34 | |
| 35 | // Create an array for each rail |
| 36 | const fence = Array(rails).fill().map(() => []); |
| 37 | |
| 38 | let rail = 0; |
| 39 | let step = 1; |
| 40 | |
| 41 | // Distribute characters across rails in a zigzag pattern |
| 42 | for (let i = 0; i < cleanMsg.length; i++) { |
| 43 | fence[rail].push(cleanMsg[i]); |
| 44 | |
| 45 | // Change direction at the top and bottom rails |
| 46 | if (rail === 0) { |
| 47 | step = 1; |
| 48 | } else if (rail === rails - 1) { |
| 49 | step = -1; |
| 50 | } |
| 51 | |
| 52 | rail += step; |
| 53 | } |
| 54 | |
| 55 | // Concatenate all rails to form the encoded message |
| 56 | return fence.map(row => row.join('')).join(''); |
| 13 | 57 | }; |
| 14 | 58 | |
| 15 | 59 | /** |
| 17 | 61 | * @param {number} rails |
| 18 | 62 | * @returns {string} |
| 19 | 63 | */ |
| 20 | | export const decode = () => { |
| 21 | | throw new Error('Remove this line and implement the function'); |
| 64 | export const decode = (msg, rails) => { |
| 65 | // Edge Case: Handle null or undefined message |
| 66 | if (msg === null || msg === undefined) { |
| 67 | return ''; |
| 68 | } |
| 69 | |
| 70 | // Edge Case: Handle zero or negative rails |
| 71 | if (rails <= 0) { |
| 72 | return ''; |
| 73 | } |
| 74 | |
| 75 | // Edge Case: Handle single rail |
| 76 | if (rails === 1) { |
| 77 | return msg; |
| 78 | } |
| 79 | |
| 80 | // Edge Case: Handle more rails than message length |
| 81 | if (rails > msg.length) { |
| 82 | return msg; |
| 83 | } |
| 84 | |
| 85 | // Edge Case: Handle empty message |
| 86 | if (msg.length === 0) { |
| 87 | return ''; |
| 88 | } |
| 89 | |
| 90 | // Create a fence to map the zigzag pattern |
| 91 | const fence = Array(rails).fill().map(() => Array(msg.length).fill(null)); |
| 92 | |
| 93 | let rail = 0; |
| 94 | let step = 1; |
| 95 | |
| 96 | // Mark the positions in the zigzag pattern |
| 97 | for (let i = 0; i < msg.length; i++) { |
| 98 | fence[rail][i] = true; |
| 99 | |
| 100 | if (rail === 0) { |
| 101 | step = 1; |
| 102 | } else if (rail === rails - 1) { |
| 103 | step = -1; |
| 104 | } |
| 105 | |
| 106 | rail += step; |
| 107 | } |
| 108 | |
| 109 | // Fill the fence with characters from the encoded message |
| 110 | const result = Array(msg.length).fill(null); |
| 111 | let charIndex = 0; |
| 112 | |
| 113 | for (let r = 0; r < rails; r++) { |
| 114 | for (let i = 0; i < msg.length; i++) { |
| 115 | if (fence[r][i] === true) { |
| 116 | result[i] = msg[charIndex]; |
| 117 | charIndex++; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return result.join(''); |
| 22 | 123 | }; |
| 124 | |
| 125 | // Handled Edge Cases: null/undefined message, zero/negative rails, single rail, more rails than message length, empty message |