-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesarlong.js
More file actions
44 lines (42 loc) · 1.14 KB
/
caesarlong.js
File metadata and controls
44 lines (42 loc) · 1.14 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
function caesarCipher(s, k) {
let answer = "";
for (let i in s){
// console.log(s[i]);
answer += cypherLetter(s[i],k);
}
return answer;
}
function cypherLetter(chr,d){
while (d > 26){
d -= 26;
}
let dis = d;
let alphabet = "abcdefghijklmnopqrstuvwxyz";
if (chr.toUpperCase() === chr){
alphabet = alphabet.toUpperCase();
}
// if (isNaN(chr)){
// alphabet = "1234567890";
// }
let answer = "";
let size = alphabet.length - 1;
let dislocation = 0;
let newPosition = 0;
let found = false;
for (let i in alphabet){
if (alphabet[i] == chr) {
dislocation = parseInt(i) + dis;
if (dislocation > size && dislocation > 0){
newPosition = dislocation <= size ? dislocation : (while (dislocation > size) { dislocation dislocation - size - 1); });
} else {
newPosition = dislocation <= size ? dislocation : (dislocation - size)
}
answer = alphabet[newPosition];
found = true;
break;
}
}
return found ? answer : chr;
}
console.log(caesarCipher("xyz", 2)); // "zab"
console.log(caesarCipher("xyz", -2)); // "yza"