Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add Rail Fence cipher implementation in JavaScript
  • Loading branch information
DivyaMadane committed Jan 8, 2026
commit fbcc54cadf810c8a61a79eba7eded916b3df4981
20 changes: 20 additions & 0 deletions Ciphers/RailFenceCipher.js
Original file line number Diff line number Diff line change
@@ -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;
Loading