Javascript 解码莫尔斯电码

Javascript 解码莫尔斯电码,javascript,morse-code,Javascript,Morse Code,对于这个项目,我试图解码一个给定的莫尔斯电码字符串。编码字符由一个空格分隔,单词由三个空格分隔。我很难通过单词空格。我一直收到“未定义的单词” decodeMorse=函数(莫尔斯电码){ 输出=”; 对于(变量i=0;i“嘿,伙计” 很抱歉这些奇怪的引语。它不会显示没有外部循环的空间。可能会嵌套两个循环。外循环将摩尔斯电码拆分为三个空格,内循环将单词拆分为一个空格。这将解析出字母,然后您可以使用莫尔斯电码字母的枚举映射字母数字字符 //message=Halp!摩尔斯电码快把我逼疯了! va

对于这个项目,我试图解码一个给定的莫尔斯电码字符串。编码字符由一个空格分隔,单词由三个空格分隔。我很难通过单词空格。我一直收到“未定义的单词”

decodeMorse=函数(莫尔斯电码){
输出=”;
对于(变量i=0;i
示例:“……-”--“——”——>“嘿,伙计”
很抱歉这些奇怪的引语。它不会显示没有外部循环的空间。

可能会嵌套两个循环。外循环将摩尔斯电码拆分为三个空格,内循环将单词拆分为一个空格。这将解析出字母,然后您可以使用莫尔斯电码字母的枚举映射字母数字字符

//message=Halp!摩尔斯电码快把我逼疯了!
var message=“…-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-”;
var字母表={
"-----":"0",
".----":"1",
"..---":"2",
"...--":"3",
"....-":"4",
".....":"5",
"-....":"6",
"--...":"7",
"---..":"8",
"----.":"9",
“-”:“a”,
“-…”:“b”,
“-.-”:“c”,
“-……”:“d”,
“:“e”,
“.-”:“f”,
“-”:“g”,
“…”:“h”,
“:”我“,
“----”:“j”,
“-.-”:“k”,
“-…”:“l”,
“-”:“m”,
-.:“n”,
“——”:“o”,
“-”:“p”,
“--.-”:“q”,
“-”:“r”,
“…”:“s”,
“-”:“t”,
“.-”:“u”,
“…-”:“v”,
“-”:“w”,
“-…-”:“x”,
“-.-”:“y”,
“…”:“z”,
"/":" ",
"-.-.--":"!",
".-.-.-":".",
"--..--":","
};
var messageConverted=[];
message.split(“”).map(函数(word){
word.split(“”).map(函数(字母){
messageConverted.push(字母[字母]);
});
messageConverted.push(“”);
});

console.log(messageConverted.join(“”)可能会嵌套两个循环。外循环将摩尔斯电码拆分为三个空格,内循环将单词拆分为一个空格。这将解析出字母,然后您可以使用莫尔斯电码字母的枚举映射字母数字字符

//message=Halp!摩尔斯电码快把我逼疯了!
var message=“…-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-”;
var字母表={
"-----":"0",
".----":"1",
"..---":"2",
"...--":"3",
"....-":"4",
".....":"5",
"-....":"6",
"--...":"7",
"---..":"8",
"----.":"9",
“-”:“a”,
“-…”:“b”,
“-.-”:“c”,
“-……”:“d”,
“:“e”,
“.-”:“f”,
“-”:“g”,
“…”:“h”,
“:”我“,
“----”:“j”,
“-.-”:“k”,
“-…”:“l”,
“-”:“m”,
-.:“n”,
“——”:“o”,
“-”:“p”,
“--.-”:“q”,
“-”:“r”,
“…”:“s”,
“-”:“t”,
“.-”:“u”,
“…-”:“v”,
“-”:“w”,
“-…-”:“x”,
“-.-”:“y”,
“…”:“z”,
"/":" ",
"-.-.--":"!",
".-.-.-":".",
"--..--":","
};
var messageConverted=[];
message.split(“”).map(函数(word){
word.split(“”).map(函数(字母){
messageConverted.push(字母[字母]);
});
messageConverted.push(“”);
});

console.log(messageConverted.join(“”)这里有一个使用
.map()
.split()
.join()
的方法

功能解码莫尔斯电码(莫尔斯电码){
var ref={
“.-”:“a”,
“-…”:“b”,
“-.-”:“c”,
“-…”:“d”,
“:”e“,
“…-”:“f”,
“-”:“g”,
“…”:“h”,
“…”:“我”,
“.----”:“j”,
“-.-”:“k”,
“.-…”:“l”,
“——”:“m”,
“-”:“n”,
“——”:“o”,
“.-”:“p”,
“——”:“q”,
“.-”:“r”,
“…”:“s”,
“-”:“t”,
“…-”:“u”,
“…-”:“v”,
“.-”:“w”,
“-…-”:“x”,
“-。-”:“y”,
“…”:“z”,
'.----':  '1',
'..---':  '2',
'...--':  '3',
'....-':  '4',
'.....':  '5',
'-....':  '6',
'--...':  '7',
'---..':  '8',
'----.':  '9',
'-----':  '0',
};
返回莫尔斯电码
.拆分(“”)
.地图(
a=>a
.拆分(“”)
.地图(
b=>ref[b]
).加入(“”)
).加入(“”);
}
var decoded=decodeMorse(“.--.--.--.--.--.--.--.--.--”);

控制台日志(已解码)这里有一个使用
.map()
.split()
.join()
的方法

功能解码莫尔斯电码(莫尔斯电码){
var ref={
“.-”:“a”,
“-…”:“b”,
“-.-”:“c”,
“-…”:“d”,
“:”e“,
“…-”:“f”,
“-”:“g”,
“…”:“h”,
“…”:“我”,
“.----”:“j”,
“-.-”:“k”,
“.-…”:“l”,
“——”:“m”,
“-”:“n”,
“——”:“o”,
“.-”:“p”,
“——”:“q”,
“.-”:“r”,
“…”:“s”,
“-”:“t”,
“…-”:“u”,
“…-”:“v”,
“.-”:“w”,
“-…-”:“x”,
“-。-”:“y”,
“…”:“z”,
'.----':  '1',
'..---':  '2',
'...--':  '3',
'....-':  '4',
'.....':  '5',
'-....':  '6',
'--...':  '7',
'---..':  '8',
'----.':  '9',
'-----':  '0',
};
返回莫尔斯电码
.拆分(“”)
.地图(
a=>a
.拆分(“”)
.地图(
b=>ref[b]
).加入(“”)
).加入(“”);
}
var decoded=decodeMorse(“.--.--.--.--.--.--.--.--.--”);
控制台日志(已解码)莫尔斯电文示例:

var message = ".... .- .-.. .--. -.-.--     -- --- .-. ... .     -.-. --- -.. .     .. ...     -.. .-. .. ...- .. -. --.     -- .     -. ..- - ... -.-.--"; 
莫尔斯字典:

var dictionary= {  
   "-----":"0",
   ".----":"1",
   "..---":"2",
   "...--":"3",
   "....-":"4",
   ".....":"5",
   "-....":"6",
   "--...":"7",
   "---..":"8",
   "----.":"9",
   ".-":"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",
   "-.-.--":"!",
   ".-.-.-":".",
   "--..--":","
};
搜索以
-
开头的图案,并将其翻译为:

var representation = message.replace(/([.-]+[-.]*)/g, (_, x) =>dictionary [x]);
console.log(representation);
莫尔斯电文示例:

var message = ".... .- .-.. .--. -.-.--     -- --- .-. ... .     -.-. --- -.. .     .. ...     -.. .-. .. ...- .. -. --.     -- .     -. ..- - ... -.-.--"; 
莫尔斯字典:

var dictionary= {  
   "-----":"0",
   ".----":"1",
   "..---":"2",
   "...--":"3",
   "....-":"4",
   ".....":"5",
   "-....":"6",
   "--...":"7",
   "---..":"8",
   "----.":"9",
   ".-":"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",
   "-.-.--":"!",
   ".-.-.-":".",
   "--..--":","
};
搜索以
-
开头的图案,并将其翻译为:

var representation = message.replace(/([.-]+[-.]*)/g, (_, x) =>dictionary [x]);
console.log(representation);

另一个基于2个循环的解决方案。莫尔斯电码中的分词
decodeMorse = function( morseCode ) {
    return morseCode
             .split("   ") // get word code 3 spaces apart
             .map(word => word
                           .split(" ") // get character code 1 spaces apart
                           .map(character => MORSE_CODE[character]) // decode Morse code character
                           .join('')
              )
              .join(' ') // add spaces between words 
              .trim()
}

decodeMorse('.... . -.--   .--- ..- -.. .') // "HEY JUDE"
decodeMorse(' . ') // "E"
    var MORSE_CODE = {  
        "-----":"0",
        ".----":"1",
        "..---":"2",
        "...--":"3",
        "....-":"4",
        ".....":"5",
        "-....":"6",
        "--...":"7",
        "---..":"8",
        "----.":"9",
        ".-":"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 MorseCode = str => {

    var dictionary = {
        "-----": "0",
        ".----": "1",
        "..---": "2",
        "...--": "3",
        "....-": "4",
        ".....": "5",
        "-....": "6",
        "--...": "7",
        "---..": "8",
        "----.": "9",
        ".-": "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",
        "-.-.--": "!",
        ".-.-.-": ".",
        "--..--": ","
    };   
    var words = ''
    str.split(" ").forEach(cur => {
        if(cur === ""){
            words += " "
        }else{
            if(dictionary[cur]){
                words += dictionary[cur]
            }
        } 
    });
    return (words.split("  ")).join(" ")
}

const getPhrase = MorseCode(".... . -.--   .--- ..- -.. .")


console.log(getPhrase);