-
Notifications
You must be signed in to change notification settings - Fork 0
/
Caesar Cipher - javascript
25 lines (23 loc) · 1.04 KB
/
Caesar Cipher - javascript
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
https://www.hackerrank.com/contests/mountblue-technologies/challenges/caesar-cipher-1
let input = function caesar(s, k){
var output = '';
for(let i = 0; i < s.length; i++){
var chars = s[i];
This line is kinda unnecessary
//if(chars.match(/[a-z]/i)){
var charnum = s.charCodeAt(i);
if(charnum >= 65 && charnum <= 90){
chars = String.fromCharCode(((charnum - 65 + k) % 26) + 65);
}
if(charnum >= 97 && charnum <= 122){
chars = String.fromCharCode(((charnum - 97 + k) % 26) + 97)
}
//}
output += chars;
console.log(`chars = ${chars}, charnum = ${charnum}, output = ${output}`)
}
console.log(output);
return output;
}
input('middle-Outz',4)
Each character is iterated and along with its charnumbers , if any char number satisfies the condition they will be substituted with respect to key and stored (replaced) in a char iterator variable and they are all added to an output.