window.ENTITIES={'/api/snippets/javascript/javascript%20-%20decode%20morse%20code%20(international%20itu%20standard)':[{"result":true,"message":null,"batch":{"type":"javascript","name":"javascript - decode morse code (international itu standard)","items":[{"id":"jm5kAj","type":"javascript","name":"JavaScript - decode Morse Code (International ITU standard)","content":"// ONLINE-RUNNER:browser;\n\n// Note: to find more optimal solution go to https://dirask.com/snippets/JavaScript-decode-Morse-Code-International-ITU-standard-1e5741\n\nvar CODES = { \n // Latin alphabet\n '.-': 'a',\n '-...': 'b',\n '-.-.': 'c',\n '-..': 'd',\n '.': 'e',\n '..-.': 'f',\n '--.': 'g',\n '....': 'h',\n '..': 'i',\n '.---': 'j',\n '-.-': 'k',\n '.-..': 'l',\n '--': 'm',\n '-.': 'n',\n '---': 'o',\n '.--.': 'p',\n '--.-': 'q',\n '.-.': 'r',\n '...': 's',\n '-': 't',\n '..-': 'u',\n '...-': 'v',\n '.--': 'w',\n '-..-': 'x',\n '-.--': 'y',\n '--..': 'z',\n // Arabic numerals\n '.----': '1',\n '..---': '2',\n '...--': '3',\n '....-': '4',\n '.....': '5',\n '-....': '6',\n '--...': '7',\n '---..': '8',\n '----.': '9',\n '-----': '0',\n // Punctuation marks and symbols\n '.-.-.-': '.',\n '--..--': ',',\n '.----.': \"'\",\n '.-..-.': '\"',\n '..--.-': '_', // Nonstandard punctuation\n '---...': ':',\n '-.-.-.': ';', // Nonstandard punctuation\n '..--..': '?',\n '-.-.--': '!', // Nonstandard punctuation\n '-....-': '-',\n '.-.-.': '+',\n '-..-.': '/',\n '-.--.': '(',\n '-.--.-': ')',\n '-...-': '=',\n '.--.-.': '@',\n '.-...': '&', // Nonstandard punctuation\n '...-..-': '$' // Nonstandard punctuation\n};\n\nvar LETTER_EXPRESSION = / /;\nvar WORD_EXPRESSION = / [ /] /;\n\nfunction decodeLetter(code) {\n var letter = CODES[code];\n if (letter == null) {\n throw new Error('Indicated Morse Code is incorrect.');\n }\n return letter;\n}\n\nfunction decodeWord(code) {\n return code\n .split(LETTER_EXPRESSION)\n .map(decodeLetter)\n .join('');\n}\n\nfunction decodeMorse(code) {\n return code\n .split(WORD_EXPRESSION)\n .map(decodeWord)\n .join(' ');\n}\n\n\n\n// Usage example:\n\nvar code = '.... . .-.. .-.. --- .-- --- .-. .-.. -..'; // OR: '.... . .-.. .-.. --- / .-- --- .-. .-.. -..'\nvar text = decodeMorse(code);\n\nconsole.log(text); // hello world\n\n\n\n// References:\n//\n// 1. https://en.wikipedia.org/wiki/Morse_code","source":"","author":{"id":"eaBBla","name":"FryerTuck","avatar":"1629139808762__eaBBla__w40px_h40px.jpg","points":649,"role":"BASIC"},"creationTime":1732416118000,"updateTime":1732574095000,"removalTime":null},{"id":"1e5741","type":"javascript","name":"JavaScript - decode Morse Code (International ITU standard)","content":"// ONLINE-RUNNER:browser;\n\nvar CODES = { \n // Latin alphabet\n '.-': 'a',\n '-...': 'b',\n '-.-.': 'c',\n '-..': 'd',\n '.': 'e',\n '..-.': 'f',\n '--.': 'g',\n '....': 'h',\n '..': 'i',\n '.---': 'j',\n '-.-': 'k',\n '.-..': 'l',\n '--': 'm',\n '-.': 'n',\n '---': 'o',\n '.--.': 'p',\n '--.-': 'q',\n '.-.': 'r',\n '...': 's',\n '-': 't',\n '..-': 'u',\n '...-': 'v',\n '.--': 'w',\n '-..-': 'x',\n '-.--': 'y',\n '--..': 'z',\n // Arabic numerals\n '.----': '1',\n '..---': '2',\n '...--': '3',\n '....-': '4',\n '.....': '5',\n '-....': '6',\n '--...': '7',\n '---..': '8',\n '----.': '9',\n '-----': '0',\n // Punctuation marks and symbols\n '.-.-.-': '.',\n '--..--': ',',\n '.----.': \"'\",\n '.-..-.': '\"',\n '..--.-': '_', // Nonstandard punctuation\n '---...': ':',\n '-.-.-.': ';', // Nonstandard punctuation\n '..--..': '?',\n '-.-.--': '!', // Nonstandard punctuation\n '-....-': '-',\n '.-.-.': '+',\n '-..-.': '/',\n '-.--.': '(',\n '-.--.-': ')',\n '-...-': '=',\n '.--.-.': '@',\n '.-...': '&', // Nonstandard punctuation\n '...-..-': '$' // Nonstandard punctuation\n};\n\nfunction decodeLetter(code) {\n var result = CODES[code];\n if (result == null) { // null or undefined\n throw new Error('Indicated Morse Code is incorrect.');\n }\n return result;\n}\n\nfunction decodeMorse(code) {\n var result = '';\n if (code) {\n var key = '';\n for (var i = 0; i < code.length; ++i) {\n var symbol = code[i];\n if (symbol === ' ') {\n result += decodeLetter(key);\n key = '';\n if (++i >= code.length) {\n break;\n }\n symbol = code[i];\n if (symbol === ' ' || symbol === '/') {\n if (++i >= code.length) {\n break;\n }\n symbol = code[i];\n if (symbol === ' ') {\n result += ' ';\n continue;\n }\n throw new Error('Indicated Morse Code is incorrect.');\n }\n }\n key += symbol;\n }\n result += decodeLetter(key);\n }\n return result;\n}\n\n\n\n// Usage example:\n\nvar code = '.... . .-.. .-.. --- .-- --- .-. .-.. -..';\nvar text = decodeMorse(code);\n\nconsole.log(text); // hello world\n\n\n\n// References:\n//\n// 1. https://en.wikipedia.org/wiki/Morse_code","source":"","author":{"id":"Ro42dD","name":"Creg","avatar":"1667335677213__Ro42dD__w40px_h40px.jpg","points":9600,"role":"ADMIN"},"creationTime":1732464877000,"updateTime":1732558802000,"removalTime":null}]}}]};