Web Programming

Codings

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

Tasks

  1. Implement a constructor function CharCoding(char, code) which creates objects having a character and an associated code.
  2. Implement a constructor function Coding(name, charCodings, delimiter):
    • name is the name of the coding (e.g. "Morse")
    • charCodings is an array of character codings
    • delimiter is the delimiter used to separate charcater codings
  3. 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
  4. Use the Morse alphabet to define an array of character codings and use the array to create a morseCoding object.
  5. Implement additional codings, for example a hexCoding that converts characters into their hexadecimal code or a caesarCoding that shifts each letter by one.
  6. 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
  7. (Optional) Install the Jest testing framework and use the script codings.test.js to test the encoding automatically:
    npm install jest
    npx jest

Solution