From fbcc54cadf810c8a61a79eba7eded916b3df4981 Mon Sep 17 00:00:00 2001 From: DivyaMadane Date: Thu, 8 Jan 2026 15:41:59 +0530 Subject: [PATCH 1/2] Add Rail Fence cipher implementation in JavaScript --- Ciphers/RailFenceCipher.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Ciphers/RailFenceCipher.js diff --git a/Ciphers/RailFenceCipher.js b/Ciphers/RailFenceCipher.js new file mode 100644 index 0000000000..124a9b0883 --- /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; From dcdbdc80363c48a75f3319336f4b9facfd9f8a6a Mon Sep 17 00:00:00 2001 From: DivyaMadane Date: Thu, 8 Jan 2026 16:01:56 +0530 Subject: [PATCH 2/2] Fix code style issues using Prettier --- Ciphers/RailFenceCipher.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Ciphers/RailFenceCipher.js b/Ciphers/RailFenceCipher.js index 124a9b0883..6db01b46b3 100644 --- a/Ciphers/RailFenceCipher.js +++ b/Ciphers/RailFenceCipher.js @@ -1,20 +1,20 @@ function railFenceCipher(text, rails) { - if (rails <= 1) return text; + if (rails <= 1) return text - const fence = Array.from({ length: rails }, () => []); - let rail = 0; - let direction = 1; + const fence = Array.from({ length: rails }, () => []) + let rail = 0 + let direction = 1 for (const char of text) { - fence[rail].push(char); - rail += direction; + fence[rail].push(char) + rail += direction if (rail === 0 || rail === rails - 1) { - direction *= -1; + direction *= -1 } } - return fence.flat().join(''); + return fence.flat().join('') } -module.exports = railFenceCipher; +module.exports = railFenceCipher