-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
64 lines (56 loc) · 2.23 KB
/
script.js
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
document.addEventListener("DOMContentLoaded", function () {
document.getElementById('convertToMorse').addEventListener('click', function () {
var inputText = document.getElementById('inputText').value;
var text = (inputText);
document.getElementById('outputMorse').value = textToMorse(text);
});
});
document.addEventListener("DOMContentLoaded", function () {
document.getElementById('convertToText').addEventListener('click', function () {
var inputMorse = document.getElementById('inputMorse').value;
var morse = (inputMorse);
document.getElementById('outputText').value = morseToText(morse);
});
});
function textToMorse(text) {
var morseCode = '';
for (var i = 0; i < text.length; i++) {
var character = text.charAt(i);
if (character === ' ') {
morseCode += ' ';
} else {
morseCode += morseCodeMap[character.toUpperCase()] + ' ';
}
}
return morseCode;
}
function morseToText(morseCode) {
var text = '';
var morseCodeWords = morseCode.split(' ');
console.log(morseCodeWords);
for (var i = 0; i < morseCodeWords.length; i++) {
var morseCodeWord = morseCodeWords[i];
console.log(morseCodeWord);
if (morseCodeWord === '') {
text += ' ';
} else {
var character = '';
for (var key in morseCodeMap) {
console.log(key);
if (morseCodeMap[key] === morseCodeWord) {
character += key;
break;
}
}
text += character;
}
}
return text;
}
var morseCodeMap = {
'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': '--..',
'0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.',
', ': '--..--', '.': '.-.-.-', '?': '..--..', '/': '-..-.', '-': '-....-', '(': '-.--.', ')': '-.--.-'
};