Skip to content

Instantly share code, notes, and snippets.

@tonyonodi
Created April 17, 2017 15:04
Show Gist options
  • Save tonyonodi/4624df0e44ee58952a1494f6bc7baa60 to your computer and use it in GitHub Desktop.
Save tonyonodi/4624df0e44ee58952a1494f6bc7baa60 to your computer and use it in GitHub Desktop.
Quick morse code reader
const morseMap = {
'.-': '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',
}
const readMorseWord = word => word
.split(/\s/)
.map(letter => morseMap[letter])
.join('');
const readMorse = code => code
.split(/\s{8}/)
.map(readMorseWord)
.join(' ')
readMorse('-- --- .-. ... . -.-. --- -.. .')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment