How to Morse Code Decoded in Javascript?
I need a function that takes a string (morse code) as an argument and returns an unencrypted string.
1 Answer
Here you can send a morse code as a string and the function will return an unencrypted string.
function decodeMorse(str) {
var ref = {
'.-': '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',
'.----': '1',
'..---': '2',
'...--': '3',
'....-': '4',
'.....': '5',
'-....': '6',
'--...': '7',
'---..': '8',
'----.': '9',
'-----': '0',
' ': ' ',
'-.-.--': '!',
'--..--': ',',
'.-.-.-': '.',
'.----.': '\'',
'---...': ':',
'..--..': '?'
};
return str
.split(' ')
.map(
elem => elem
.split(' ')
.map(
b => ref[b]
).join('')
).join(' ');
}
Example
decodeMorse('--') //"M"
answer Link