-
Notifications
You must be signed in to change notification settings - Fork 3
/
decrypt.js
47 lines (37 loc) · 1.06 KB
/
decrypt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* ==========================================================================
*
* Use:
* "HWe o!lordll".decrypt(4)
* => "Hello World!"
*
* ========================================================================== */
String.prototype.decrypt = function(rows = 3) {
rows = rows || 3
let fence = [];
for (let i = 0; i < rows; i++) fence.push([])
let rail = 0;
let change = 1;
this.split("").forEach(char => {
fence[rail].push(char)
rail += change
if (rail === rows - 1 || rail === 0) change = -change
})
const rFence = [];
for (let i = 0; i < rows; i++) rFence.push([])
i = 0
let s = this.split("")
for (r of fence) {
for (let j = 0; j < r.length; j++) rFence[i].push(s.shift())
i++
}
rail = 0
change = 1
var r = ""
for (var i = 0; i < this.length; i++) {
r += rFence[rail].shift()
rail += change
if (rail === rows - 1 || rail === 0) change = -change
}
return r
}
module.exports = (text, rows = 3) => text.decrypt(rows);