-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesars Cipher
More file actions
25 lines (22 loc) · 749 Bytes
/
Caesars Cipher
File metadata and controls
25 lines (22 loc) · 749 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
const alphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
function rot13(str) {
//create accumulator
let accumulator = '';
//loop through the string
for(let i=0; i < str.length; i++){
//if character is not letter then simply add to accumulator
const char = str[i];
const isALetter = alphabet.includes(char);
if (isALetter===false){
accumulator += char;
}
// else substract or add 13 indices and add to accumulator
else{
const charIndex = alphabet.findIndex((c) => c === char);
accumulator+= alphabet [charIndex+13] || alphabet[charIndex-13];
}
}
//return accumulator
return accumulator;
}
rot13("SERR PBQR PNZC");