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
-
Implement a constructor function
CharCoding(char, code)
which creates objects having a character and an associated code.
-
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
-
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