The first part of this homework involves writing JavaScript, along with some HTML to hold it. Follow the same instructions as in HW 1 -- write code by hand, make it easy to read, print it from the text editor, timestamp it, and upload it to your private folder. For both problems, you are being provided with some code. Edit and and extend it, and then turn in the whole thing (not just your part). Indicate where you have modified the provided code.
The second part of this homework is a set of written questions. You are not required to write your answers in HTML. However, you should still upload them to your private directory and provide a printout. Upload your file in a format we can read -- plain text, HTML, PDF, Word, or RTF. No timestamp is required.
There are three levels: the original message, the individual packets, and the reassembled message. At the top level is a text box in which the original message is entered, and a text box in which the packet size is entered. Pressing a button then "packetizes" the message (splits it into packets of the size), for display in the second level. Pressing another button "reassembles" whatever is in the textarea for display in the third level. Note that the packetized message may be modified (e.g., removing, reordering, or duplicating packets) before reassembly.
packetize function is missing most of its body.
Add code to the body to break the given message into packets of the
given size. The final packet may be smaller than the given size.reassemble function also needs your help. Add
code to piece together the lines from the packetized textarea into a
single string. The code should detect and give an alert when a packet
is missing or duplicated. It should transparently handle out-of-order
packets, reassembling correctly regardless of order.Some hints:
getArray function (originally seen in the
JavaScript sorting example) is provided, and used to extract an array
from the textarea.pad3 function will pad a
number into a 3-character string, which can be stuck on the front of
the packet body. To convert such a 3-character string back to a
number, you need to tell parseInt that it's decimal;
e.g., parseInt(string, 10).join it together, as was done at the end of
packetize, but glueing together with a blank string
rather than a newline.charAt (works like indexing an array)
and substr (getting just a portion of a string).For extra credit (only do this once the whole rest of the homework is in great shape, and only if you want to), provide additional functionality, such as a checksum for error detection.
true for 1, false for 0). This exactly
parallels an HTML table structure. The newPicture
function creates the array and the table, based on some size
parameters. It also sets up the naming so that the array and table
are cross-referenced (see setColor, toggle,
etc.).Extend the code to provide save and open functionality. Rather
than dealing with a file, just "save" to a text box or textarea and
get back from it. The exact format of how you do that is up to you,
but I would recommend basing it on a string of 0s and 1s (using
charAt to get each one). The functions should complement
each other -- when you save something, do some more pixel
manipulation, and then load, you should be right back where you
were.
For extra credit (only do this once the whole rest of the homework is in great shape, and only if you want to), provide additional functionality, such as colors rather than just black-and-white, the ability to draw lines or text, etc.
Some hints:
clear.
The outer loop goes row-by-row, and the inner loop goes across the
columns in a row.getArray from
the first problem to load it back into an array. Each element in the
array is then a string corresponding to what you wrote out for the
corresponding row.