Encode Morse in javaScript?
Notes - Input value can be lower or upper case. - Input string can have digits. - Input string can have some special characters (e.g. comma, colon, apostrophe, period, question mark, exclamation mark).
1 Answer
Enjoy trying this function
const conversionMap = {
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: '--..',
0: '-----',
1: '.----',
2: '..---',
3: '...--',
4: '....-',
5: '.....',
6: '-....',
7: '--...',
8: '---..',
9: '----.',
',': '--..--',
':': '---...',
"'": '.----.',
'.': '.-.-.-',
'?': '..--..',
'!': '-.-.--',
' ': ' ',
};
const encodeMorse = str =>
str
.split('')
.map(char => conversionMap[char.toUpperCase()])
.join(' ');
answer Link