-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesar.js
30 lines (26 loc) · 877 Bytes
/
caesar.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
function caesarCipher(inputString, key)
{
var 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"];
var keyValue = key;
var inputStringArray = inputString.split();
for (var i = 0; i < inputStringArray.length; i++)
{
for (var j = 0; j < alphabet.length; j++)
{
if (inputStringArray[i] === alphabet[j])
{
if (j + keyValue > 25)
{
var cipher = (j + keyValue) - 25;
inputStringArray[i] = alphabet[cipher];
}
else
{
inputStringArray[i] = alphabet[j + key];
}
}
}
}
var newString = inputStringArray.join();
return newString;
}