Web Programming

Various Codings

The objective of this exercise is to write a script codings.js that allows a text to be encoded with various codings.

Tasks

  1. Implement a constructor function Coding(name, charCodings, delimiter):
    • name is the name of the coding (e.g. "Morse")
    • charCodings is an object (associative array) with the character codings
    • delimiter is the delimiter used to separate character codings
  2. Add the following functions to the prototype of the Coding function:
    • encodeChar(char) returns the encoding of a single character
    • encode(text) returns the encoding of a text
  3. Use the Morse alphabet to define an array of character codings and use the array to create a morseCoding object.
  4. Implement additional codings, for example a hexCoding that converts characters into their hexadecimal code, or a caesarCoding that shifts each letter by one.
  5. Add the statement module.exports = { morseCoding, hexCoding, caesarCoding }; to your script and use the script main.js to test the encoding manually with Node.js:
    node main.js
  6. (Optional) Install the Jest testing framework and use the script codings.test.js to test the encoding automatically:
    npm install jest
    npx jest

Solution