-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption.js
45 lines (40 loc) · 1.11 KB
/
encryption.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
/*
* Complete the 'encryption' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
function encryption(s) {
// Write your code here
let lengthS = 0;
let valMin = 0;
let valMax = 0;
let tempString = '';
let valSlice = 0;
let tabString = [];
let res = '';
s = s.replace(/ +/g, "");
lengthS = s.length;
lengthS = Math.sqrt(lengthS);
valMin = Math.floor(lengthS);
valMax = Math.ceil(lengthS);
valSlice = valMax;
for(let i=0; i<s.length; i+=valMax){
tempString = s.slice(i, valSlice);
tabString.push(tempString);
valSlice += valMax;
}
for(let j=0; j<tabString[0].length; j++){
for(let i=0; i<tabString.length; i++){
if(tabString[i][j]){
res += tabString[i][j];
}
}
res += ' ';
}
return res;
}
module.exports = encryption;
let s = "have a nice day"; //hae and via ecy
// let s = "i ffact s dontf i ttoth e orych a ngeth e facts"; //isieae fdtonf fotrga anoyec cttctt tfhhhs
console.log(encryption(s));