-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrow-column-based-algorithm.js
More file actions
77 lines (62 loc) · 2 KB
/
Copy pathrow-column-based-algorithm.js
File metadata and controls
77 lines (62 loc) · 2 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
let Coder = function(SourceData, size){
this.SourceData = SourceData;
this.size = size;
this.Encoder = function(){
let arr = SourceData.split('');
let l = 0;
let map = [];
let rowsize = size;
if(arr.length > Math.pow(size,2)){
rowsize = size + Math.floor((arr.length - Math.pow(size,2))/size);
} else if(arr.length < Math.pow(size,2)){
rowsize = size - Math.floor((Math.pow(size,2) - arr.length)/size);
}
for(let i=0; i<rowsize; i++){
map[i] = [];
for(let j=0; j<size; j++){
map[i][j] = arr[l];
l++;
if(arr[l] == undefined){
arr[l] = " ";
}
}
}
l = 0;
for(let j=0; j<size; j++){
for(let i=0; i<rowsize; i++){
arr[l] = map[i][j];
l++;
}
}
return arr.join("");
}
this.Decoder = function(){
let arr = this.Encoder().split('');
let l = 0;
let map = [];
let rowsize = size;
if(arr.length > Math.pow(size,2)){
rowsize = size + Math.floor((arr.length - Math.pow(size,2))/size);
} else if(arr.length < Math.pow(size,2)){
rowsize = size - Math.floor((Math.pow(size,2) - arr.length)/size);
}
for(let i=0; i<rowsize; i++){
map[i] = [];
for(let j=0; j<size; j++){
map[i][j] = arr[l];
l++;
}
}
l = 0;
for(let j=0; j<size; j++){
for(let i=0; i<rowsize; i++){
arr[l] = map[i][j];
l++;
}
}
return arr.join("");
}
}
let hh = new Coder("Hajastani Hanrapetutjun", 5);
console.log(hh.Encoder());
console.log(hh.Decoder());