Web Programming

Morse Coding

The objective of this exercise is to write a script morseCoding.js that converts a text into Morse code.

Tasks

  1. 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=--..';
  2. 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.)
  3. 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
  4. (Optional) Install the Jest testing framework and use the script morseCoding.test.js to test the encoding automatically:
    npm install jest
    npx jest

Solution