/
CaesarCipher.js
43 lines (33 loc) · 1003 Bytes
/
CaesarCipher.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
(function () {
'use strict';
var zeroTest, zeroTest2, encodedMessage;
function decodeCaesar(word, subKey) {
var encoded = '',
alphabet = [],
index,
nextIndex;
alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
word.split('').forEach(function (ch) {
index = alphabet.indexOf(ch);
nextIndex = (index + subKey) % 26;
if(alphabet.indexOf(ch) < 0){
encoded += ' ';
} else {
encoded += alphabet[nextIndex];
}
});
return encoded;
}
zeroTest = ['mxolxv fdhvdu', 23];
zeroTest2 = ['rfgq gq y qcapcr ambc', 2];
var firsTest = {
'Code' : zeroTest[0],
'Encoded' : decodeCaesar(zeroTest[0], zeroTest[1])
};
var secondTest = {
'Code' : zeroTest2[0],
'Encoded' : decodeCaesar(zeroTest2[0], zeroTest2[1])
};
console.log(firsTest);
console.log(secondTest);
}());