diff --git a/Ciphers/RailFenceCipher.js b/Ciphers/RailFenceCipher.js new file mode 100644 index 0000000000..6db01b46b3 --- /dev/null +++ b/Ciphers/RailFenceCipher.js @@ -0,0 +1,20 @@ +function railFenceCipher(text, rails) { + if (rails <= 1) return text + + const fence = Array.from({ length: rails }, () => []) + let rail = 0 + let direction = 1 + + for (const char of text) { + fence[rail].push(char) + rail += direction + + if (rail === 0 || rail === rails - 1) { + direction *= -1 + } + } + + return fence.flat().join('') +} + +module.exports = railFenceCipher