-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.ts
More file actions
63 lines (55 loc) · 1.78 KB
/
app.ts
File metadata and controls
63 lines (55 loc) · 1.78 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Caesar {
private alphabet: string;
constructor(lang: string) {
switch (lang) {
case "ru": {
this.alphabet = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
break;
}
case "en": {
this.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
break;
}
case "ua": {
this.alphabet = "АБВГҐДЕЄЖЗИІЇЙКЛМНОПРСТУФХЦЧШЩЬЮЯ";
break;
}
default: {
this.alphabet = "";
console.log("error")
return
}
}
this.alphabet = this.alphabet.toLowerCase();
}
public addOwnAlphabet(alphabet: string): void {
this.alphabet = alphabet;
}
public encrypt(sentence: string, key: number): string {
return (this.codeEncode(sentence,key));
}
public decrypt(sentence: string, key: number): string {
return this.codeEncode(sentence,-key);
}
private codeEncode(sentence: string, key: number): string {
sentence = sentence.toLowerCase();
let letterQty = this.alphabet.length;
let retVal = "";
for (let i = 0; i < sentence.length; i++) {
let letter = sentence[i];
let index = this.alphabet.indexOf(letter);
// console.log(index)
if (index < 0) {
retVal += letter;
} else {
let codeIndex = (letterQty + index + key) % letterQty;
retVal += this.alphabet[codeIndex];
}
}
return retVal;
}
}
//main
// let instance = new Caesar("en");
// alert( instance.encrypt("sraka", 3));
// alert(instance.decrypt("vudnd",3));