-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.js
More file actions
30 lines (27 loc) · 742 Bytes
/
caesar.js
File metadata and controls
30 lines (27 loc) · 742 Bytes
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
function caesarCipher(s, k) {
let dislocation = 0;
let newMessage = "";
let current = "";
for(let i=0; i < s.length; i++){
current = s.charCodeAt(i);
if (current >=65 && current <= 90 ){
dislocation = current + k;
while(dislocation>90){
dislocation -= 26;
}
newMessage += String.fromCharCode(dislocation);
} else if (current >=97 && current <= 122 ){
dislocation = current + k;
while(dislocation>122){
dislocation -= 26;
}
newMessage += String.fromCharCode(dislocation);
} else {
newMessage += s[i];
}
}
return newMessage;
}
let message = "ZABC This is a secret message!";
let caesar = 1;
console.log(caesarCipher(message, caesar));