-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoding.js
More file actions
69 lines (60 loc) · 1.79 KB
/
Encoding.js
File metadata and controls
69 lines (60 loc) · 1.79 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
64
65
66
67
68
69
const morseAlphabet =
"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=--..; =;.=.-.-.-;,=--..--;?=..--..";
const myArray = morseAlphabet.split(";");
const map1 = new Map();
for (let i = 0; i < myArray.length; i++) {
const char = myArray[i].split("=")[0];
const morse = myArray[i].split("=")[1];
map1.set(char, morse);
}
let mapKeys = Array.from(map1.keys());
function charToMorseCode(char) {
char = char.toLowerCase();
for (let i = 0; i < myArray.length; i++) {
const check = myArray[i].split("=")[0];
if (check === char) {
return myArray[i].split("=")[1];
//console.log(myArray[i].split("=")[1]);
}
}
return "[" + char + "]";
}
function convertToMorse(phrase) {
let morsePhrase = "";
for (let i = 0; i < phrase.length; i++) {
//console.log(phrase[i]);
morsePhrase = morsePhrase.concat(" ", charToMorseCode(phrase[i]));
}
return morsePhrase.trim();
}
function Character(char, code) {
this.char = char;
this.code = code;
this.toString = function () {
return char + ": " + code;
};
}
function Encoding(name, characters, delimiter) {
this.name = name;
this.characters = characters;
this.delimiter = delimiter;
this.encode = function (text) {
let morsePhrase = "";
for (let i = 0; i < text.length; i++) {
morsePhrase = morsePhrase.concat(charToMorseCode(text[i]), delimiter);
}
return morsePhrase.trim();
};
this.toString = function () {
console.log(name + ": " + characters);
};
}
const bob = new Character("a", ".-");
const test = new Encoding("Morse", mapKeys, "/");
bob.toString();
console.log(test.encode("Hallo Welt"));
console.log(test.toString());
// map = {};
// map[chrachter.char] = char;