-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path102caesar_encode.js
More file actions
32 lines (28 loc) · 1.09 KB
/
102caesar_encode.js
File metadata and controls
32 lines (28 loc) · 1.09 KB
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
function solution(s, n) {
let answer = '';
const 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"];
if (!(s.trim().length)) return answer = s;
for (let i = 0; i < s.length; i++) {
if (!(s[i].trim().length)) {
answer += " ";
continue;
}
if (s[i].toUpperCase() === s[i]) {
answer += alphabet[alphabet.indexOf(s[i].toLowerCase()) + n > 25 ?
alphabet.indexOf(s[i].toLowerCase()) + n - 26 :
alphabet.indexOf(s[i].toLowerCase()) + n].toUpperCase();
}
if (s[i].toLowerCase() === s[i]) {
answer += alphabet[alphabet.indexOf(s[i]) + n > 25 ?
alphabet.indexOf(s[i]) + n - 26 :
alphabet.indexOf(s[i]) + n];
}
}
return answer;
}