-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesarCipher.js
More file actions
30 lines (28 loc) · 807 Bytes
/
caesarCipher.js
File metadata and controls
30 lines (28 loc) · 807 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 (str , num){
num = num % 26;
var lowerCase = str.toLowerCase();
var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
var newStr ='';
for(var i=0 ; i<lowerCase.length ; i++){
var currentLetter = lowerCase[i];
if(currentLetter === ' ')
{
newStr+= currentLetter;
continue;
}
var currentIndex = alphabet.indexOf(currentLetter);
var newIndex = currentIndex + num;
if(newIndex > 25) newIndex-= 26;
if(newIndex < 0) newIndex+=26;
if(str[i] === str[i].toUpperCase()) {
newStr+= alphabet[newIndex].toUpperCase();
}
else {
newStr += alphabet[newIndex];
}
}
return newStr;
}
// caesarCipher('Zoo Keeper' ,2);
// caesarCipher('Big Car' , -16);
caesarCipher('javascript' , -900);