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
-
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
-
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
-
Use the Morse alphabet to define an array of character codings and use the array to create a
morseCoding object.
-
Implement additional codings, for example a
hexCoding that converts characters into their hexadecimal code, or a caesarCoding that shifts each letter by one.
-
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
-
(Optional) Install the Jest testing framework and use the script codings.test.js to test the encoding automatically:
npm install jest
npx jest
Solution