This repository has been archived on 2022-02-10. You can view files and clone it, but cannot push or open issues or pull requests.
morse-converter-web/app/scripts/writtenMorseConvert.js

397 lines
12 KiB
JavaScript

var getWrittenMorseDecoded, getWrittenMorseEncoded;
getWrittenMorseDecoded = function(input) {
var output;
if (!input) {
return null;
}
if (input.charAt(input.length === " ")) {
input.substring(0, input.length - 1);
}
input = input.toUpperCase();
output = "";
if (input === "LETTERSPACE") {
return "#";
} else if (input === "END OF WORK") {
return "000101";
} else if (input === "ERROR") {
return "00000000";
} else if (input === "STARTING SIGNAL") {
return "10101";
} else if (input === "ENDING SIGNAL") {
return "01010";
} else if (input === "UNDERSTOOD") {
return "00010";
} else if (input === "WAIT") {
return "01000";
} else if (input === "SOS") {
return "000111000";
} else if (input === "LETTER SPACE") {
return "#";
} else if (input === "WORD SPACE") {
return "+";
} else {
while (input.length > 0) {
if (input.charAt(0) === " ") {
if (output.charAt(output.length) === "#") {
output = output.substring(0, output.length - 1);
}
output += "+";
input = input.substring(1);
} else if (input.charAt(0) === "\n") {
output += "<br>";
input = input.substring(1);
} else if (input.charAt(0) === "A") {
output += "01#";
input = input.substring(1);
} else if (input.charAt(0) === "B") {
output += "1000#";
input = input.substring(1);
} else if (input.charAt(0) === "C") {
output += "1010#";
input = input.substring(1);
} else if (input.charAt(0) === "D") {
output += "100#";
input = input.substring(1);
} else if (input.charAt(0) === "E") {
output += "0#";
input = input.substring(1);
} else if (input.charAt(0) === "F") {
output += "0010#";
input = input.substring(1);
} else if (input.charAt(0) === "G") {
output += "110#";
input = input.substring(1);
} else if (input.charAt(0) === "H") {
output += "0000#";
input = input.substring(1);
} else if (input.charAt(0) === "I") {
output += "00#";
input = input.substring(1);
} else if (input.charAt(0) === "J") {
output += "0111#";
input = input.substring(1);
} else if (input.charAt(0) === "K") {
output += "101#";
input = input.substring(1);
} else if (input.charAt(0) === "L") {
output += "0100#";
input = input.substring(1);
} else if (input.charAt(0) === "M") {
output += "11#";
input = input.substring(1);
} else if (input.charAt(0) === "N") {
output += "10#";
input = input.substring(1);
} else if (input.charAt(0) === "O") {
output += "111#";
input = input.substring(1);
} else if (input.charAt(0) === "P") {
output += "0110#";
input = input.substring(1);
} else if (input.charAt(0) === "Q") {
output += "1101#";
input = input.substring(1);
} else if (input.charAt(0) === "R") {
output += "010#";
input = input.substring(1);
} else if (input.charAt(0) === "S") {
output += "000#";
input = input.substring(1);
} else if (input.charAt(0) === "T") {
output += "1#";
input = input.substring(1);
} else if (input.charAt(0) === "U") {
output += "001#";
input = input.substring(1);
} else if (input.charAt(0) === "V") {
output += "0001#";
input = input.substring(1);
} else if (input.charAt(0) === "W") {
output += "011#";
input = input.substring(1);
} else if (input.charAt(0) === "X") {
output += "1001#";
input = input.substring(1);
} else if (input.charAt(0) === "Y") {
output += "1011#";
input = input.substring(1);
} else if (input.charAt(0) === "Z") {
output += "1100#";
input = input.substring(1);
} else if (input.charAt(0) === "0") {
output += "11111#";
input = input.substring(1);
} else if (input.charAt(0) === "1") {
output += "01111#";
input = input.substring(1);
} else if (input.charAt(0) === "2") {
output += "00111#";
input = input.substring(1);
} else if (input.charAt(0) === "3") {
output += "00011#";
input = input.substring(1);
} else if (input.charAt(0) === "4") {
output += "00001#";
input = input.substring(1);
} else if (input.charAt(0) === "5") {
output += "00000#";
input = input.substring(1);
} else if (input.charAt(0) === "6") {
output += "10000#";
input = input.substring(1);
} else if (input.charAt(0) === "7") {
output += "11000#";
input = input.substring(1);
} else if (input.charAt(0) === "8") {
output += "11100#";
input = input.substring(1);
} else if (input.charAt(0) === "9") {
output += "11110#";
input = input.substring(1);
} else if (input.charAt(0) === "Ä") {
output += "0101#";
input = input.substring(1);
} else if (input.charAt(0) === "Ö") {
output += "1110#";
input = input.substring(1);
} else if (input.charAt(0) === "Ü") {
output += "0011#";
input = input.substring(1);
} else if (input.charAt(0) === "ß") {
output += "00011000#";
input = input.substring(1);
} else if (input.charAt(0) === ".") {
output += "010101#";
input = input.substring(1);
} else if (input.charAt(0) === ",") {
output += "110011#";
input = input.substring(1);
} else if (input.charAt(0) === ":") {
output += "111000#";
input = input.substring(1);
} else if (input.charAt(0) === "") {
output += "101010#";
input = input.substring(1);
} else if (input.charAt(0) === "?") {
output += "001100#";
input = input.substring(1);
} else if (input.charAt(0) === "!") {
output += "101011#";
input = input.substring(1);
} else if (input.charAt(0) === "-") {
output += "100001#";
input = input.substring(1);
} else if (input.charAt(0) === "_") {
output += "001101#";
input = input.substring(1);
} else if (input.charAt(0) === "(") {
output += "10110#";
input = input.substring(1);
} else if (input.charAt(0) === ")") {
output += "101101#";
input = input.substring(1);
} else if (input.charAt(0) === "=") {
output += "10001#";
input = input.substring(1);
} else if (input.charAt(0) === "+") {
output += "01010#";
input = input.substring(1);
} else if (input.charAt(0) === "/") {
output += "10010#";
input = input.substring(1);
} else if (input.charAt(0) === "@") {
output += "011010#";
input = input.substring(1);
} else if (input.charAt(0) === "'") {
output += "011110#";
input = input.substring(1);
} else if (input.charAt(0) === "$") {
output += "0001001#";
input = input.substring(1);
} else {
return "Code not listed or wrong.";
}
}
if (output.charAt(output.length - 1) === "#") {
output = output.substring(0, output.length - 1);
}
}
return output;
};
getWrittenMorseEncoded = function(input) {
var d, inputToSign, output, sign, signFull;
if (!input) {
return null;
}
while (input.charAt(input.length - 1) === " ") {
input = input.substring(0, input.length - 1);
}
input = input.toUpperCase();
while (input.charAt(input.length - 1) === "#" || input.charAt(input.length - 1) === "+" || input.charAt(input.length - 1) === "\n") {
input = input.substring(0, input.length - 1);
}
if (input.charAt(input.length - 1) !== "#") {
input += "#";
}
inputToSign = input;
output = "";
while (inputToSign !== "#") {
d = 0;
signFull = true;
sign = "";
while (signFull) {
if (inputToSign.charAt(d) === "+" || inputToSign.charAt(d) === "#" || inputToSign.charAt(0) === "\n") {
if (d === 0) {
if (inputToSign.charAt(0) === "+") {
output += " ";
inputToSign = inputToSign.substring(1, inputToSign.length);
}
if (inputToSign.charAt(0) === "#") {
inputToSign = inputToSign.substring(1, inputToSign.length);
}
if (inputToSign.charAt(0) === "\n") {
output += "<br>";
inputToSign = inputToSign.substring(1, inputToSign.length);
}
} else {
sign = inputToSign.substring(0, d);
inputToSign = inputToSign.substring(d, inputToSign.length);
signFull = false;
}
} else {
d++;
}
}
if (sign === "01") {
output += "A";
} else if (sign === "1000") {
output += "B";
} else if (sign === "1010") {
output += "C";
} else if (sign === "100") {
output += "D";
} else if (sign === "0") {
output += "E";
} else if (sign === "0010") {
output += "F";
} else if (sign === "110") {
output += "G";
} else if (sign === "0000") {
output += "H";
} else if (sign === "00") {
output += "I";
} else if (sign === "0111") {
output += "J";
} else if (sign === "101") {
output += "K";
} else if (sign === "0100") {
output += "L";
} else if (sign === "11") {
output += "M";
} else if (sign === "10") {
output += "N";
} else if (sign === "111") {
output += "O";
} else if (sign === "0110") {
output += "P";
} else if (sign === "1101") {
output += "Q";
} else if (sign === "010") {
output += "R";
} else if (sign === "000") {
output += "S";
} else if (sign === "1") {
output += "T";
} else if (sign === "001") {
output += "U";
} else if (sign === "0001") {
output += "V";
} else if (sign === "011") {
output += "W";
} else if (sign === "1001") {
output += "X";
} else if (sign === "1011") {
output += "Y";
} else if (sign === "1100") {
output += "Z";
} else if (sign === "11111") {
output += "0";
} else if (sign === "01111") {
output += "1";
} else if (sign === "00111") {
output += "2";
} else if (sign === "00011") {
output += "3";
} else if (sign === "00001") {
output += "4";
} else if (sign === "00000") {
output += "5";
} else if (sign === "10000") {
output += "6";
} else if (sign === "11000") {
output += "7";
} else if (sign === "11100") {
output += "8";
} else if (sign === "11110") {
output += "9";
} else if (sign === "0101") {
output += "Ä";
} else if (sign === "1110") {
output += "Ö";
} else if (sign === "0011") {
output += "Ü";
} else if (sign === "00011000") {
output += "ß";
} else if (sign === "1111") {
output += "CH";
} else if (sign === "010101") {
output += ".";
} else if (sign === "110011") {
output += ",";
} else if (sign === "111000") {
output += ":";
} else if (sign === "101010") {
output += ";";
} else if (sign === "001100") {
output += "?";
} else if (sign === "101011") {
output += "!";
} else if (sign === "100001") {
output += "-";
} else if (sign === "001101") {
output += "_";
} else if (sign === "10110") {
output += "(";
} else if (sign === "101101") {
output += ")";
} else if (sign === "011110") {
output += "'";
} else if (sign === "10001") {
output += "=";
} else if (sign === "01010") {
output += "+";
} else if (sign === "10010") {
output += "/";
} else if (sign === "011010") {
output += "@";
} else if (sign === "10101") {
output += "Begin of the signal";
} else if (sign === "10001") {
output += "Wait";
} else if (sign === "00010") {
output += "Understood";
} else if (sign === "000101") {
output += "End of work";
} else if (sign === "000111000") {
output += "SOS";
} else if (sign === "00000000") {
output += "Error";
} else {
return "Code not listed or wrong.";
}
}
return output;
};