-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCeasar Cipher.js
51 lines (49 loc) · 1.38 KB
/
Ceasar Cipher.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
48
49
50
51
function rot13(str) {
let cipher = str.split(" ");
console.log("cipher: ", cipher);
const regex1 = /[A-Z]+/gi;
let found = [];
let encodedArr = [];
for(let i = 0; i<cipher.length; i++){
found.push(cipher[i].match(regex1)[0]);
}
/*
for(let i = 0; i < found.length; i++){
let encode = "";
for(let j = 0; j<found[i].length; j++){
if(found[i].charCodeAt(j) - 13 < 65){//if encoding reached a value below 65
var diff = 64 - (found[i].charCodeAt(j) - 13);
encode += String.fromCharCode(90 - diff);
}
else{
encode += String.fromCharCode(found[i].charCodeAt(j) - 13);
}
}
encodedArr.push(encode);
}
console.log(encodedArr);
return str;
}
*/
for(let i = 0; i < cipher.length; i++){
let encode = "";
for(let j = 0; j<cipher[i].length; j++){
if(cipher[i][j].match(regex1)){
if(cipher[i].charCodeAt(j) - 13 < 65){//if encoding reached a value below 65
var diff = 64 - (found[i].charCodeAt(j) - 13);
encode += String.fromCharCode(90 - diff);
}
else{
encode += String.fromCharCode(found[i].charCodeAt(j) - 13);
}
}
else{
encode += cipher[i][j];
}
}
encodedArr.push(encode);
}
//console.log(encodedArr);
return encodedArr.join(" ");
}
rot13("SERR PBQR PNZC");