-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathceaserCipher.js
More file actions
26 lines (22 loc) · 938 Bytes
/
ceaserCipher.js
File metadata and controls
26 lines (22 loc) · 938 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
// Requirement: from the given string cipher out message ahead/back number of times which is passed
function ceaserCipher(string, number) {
number = number % 26;
var stringLowerCase = string.toLowerCase().split("");
var alphabets = 'abcdefghijklmnopqrstuvwxyz'.split("");
var ciphered = "";
for (var i = 0; i < stringLowerCase.length; i++) {
var character = stringLowerCase[i]
if (character === " ") {
ciphered += character;
continue;
}
let currentIndex = alphabets.indexOf(character)
let newIndex = currentIndex + number;
if(newIndex > 25) newIndex = newIndex - 26
if(newIndex < 0) newIndex = 26 + newIndex
ciphered += alphabets[newIndex]
}
return ciphered
}
// console.log(ceaserCipher("abdul Samad", 2) + " ---> " + ceaserCipher(ceaserCipher("abdul Samad", 2), -2))
console.log(ceaserCipher("Abdul Samad", 2))