-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.js
More file actions
47 lines (40 loc) · 1.25 KB
/
cipher.js
File metadata and controls
47 lines (40 loc) · 1.25 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
45
46
47
// made an array of the alphabet twice so i can use it for the cipher. twice so i can extend beyond the first.
let alpha = ['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', '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 encipher(str, shift) {
let newWord = [];
let arr = str.split('');
for (var i = 0; i < arr.length; i++) {
let letter = arr[i];
for (var j = 0; j < alpha.length; j++) {
if (letter === alpha[j]) {
let newLetter = alpha[j + shift];
newWord.push(newLetter);
break;
};
}
}
let cipherWord = newWord.join('');
console.log(cipherWord);
return cipherWord;
};
function decipher(str, shift) {
let newWord = [];
let arr = str.split('');
for (var i = 0; i < arr.length; i++) {
let letter = arr[i];
for (var j = 26; j < alpha.length; j++) {
if (letter === alpha[j]) {
let newLetter = alpha[j - shift];
newWord.push(newLetter);
break;
}
}
}
let decipherWord = newWord.join('');
console.log(decipherWord);
return decipherWord;
};
module.exports = {
encipher,
decipher
}