forked from Avdhesh-Varshney/WebMasterLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
28 lines (25 loc) · 1.16 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
const morseToEnglishMap = {
".-": "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", ".-.-.-": ".", "--..--": ",", "..--..": "?", "-.-.--": "!"
};
function convertToEnglish() {
const morseInput = document.getElementById("morseInput").value.trim();
const morseArray = morseInput.split(" ");
let englishOutput = "";
for (let i = 0; i < morseArray.length; i++) {
if (morseToEnglishMap[morseArray[i]]) {
englishOutput += morseToEnglishMap[morseArray[i]];
} else {
document.getElementById("errorMessage").innerText = "Invalid Morse Code!";
return;
}
}
document.getElementById("englishOutput").value = englishOutput;
document.getElementById("errorMessage").innerText = "";
}