Created
April 17, 2017 15:04
-
-
Save tonyonodi/4624df0e44ee58952a1494f6bc7baa60 to your computer and use it in GitHub Desktop.
Quick morse code reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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