We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 08d8c6b commit fbcc54cCopy full SHA for fbcc54c
Ciphers/RailFenceCipher.js
@@ -0,0 +1,20 @@
1
+function railFenceCipher(text, rails) {
2
+ if (rails <= 1) return text;
3
+
4
+ const fence = Array.from({ length: rails }, () => []);
5
+ let rail = 0;
6
+ let direction = 1;
7
8
+ for (const char of text) {
9
+ fence[rail].push(char);
10
+ rail += direction;
11
12
+ if (rail === 0 || rail === rails - 1) {
13
+ direction *= -1;
14
+ }
15
16
17
+ return fence.flat().join('');
18
+}
19
20
+module.exports = railFenceCipher;
0 commit comments