Web Programming
Morse Coding
The objective of this exercise is to write a script
morseCoding.js
that converts a text into
Morse code.
Tasks
-
Use the following string to implement a function
encodeChar
which takes a character as parameter and returns the Morse code of the character as string:
const morseAlphabet =
'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=--..';
-
Implement a function
encode
which converts a text passed as parameter into Morse code. (Use the slash as a delimiter between the Morse codes of the individual characters.)
-
Add the statement
module.exports = { encodeChar, encode };
to your script and use the script main.js to test the encoding manually with Node.js:
node main.js
-
(Optional) Install the Jest testing framework and use the script morseCoding.test.js to test the encoding automatically:
npm install jest
npx jest
Solution