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.
We're going to do some circuit design. Actually, since it's a bit painful to automatically draw a circuit, we'll just design the equivalent Boolean expressions. You might want to do the relevant written problems first, to make sure you're comfortable with the ideas and techniques.
Provided in hw5-logic.html is the
basic structure of a program to take in a truth table, generate a
Boolean formula, and evaluate the formula with values for the
variables.

generateFormula. It should follow
the algorithm from class for generating a circuit / Boolean formula
from a truth table. Examine the rows in the truth table. For each
row whose checkbox is checked, add to the formula an expression for
the row. Separate the row expressions with an or symbol (+). For a
row expression, list each of the three variables (A, B, and C),
separated by AND signs (*), with a not sign (~) in front of each
variable whose truth table entry is false in that row.
Some hints:
table variable stores the input values for the
truth table. You can index it by row and column; e.g. table[0][1] has
the value of the variable B in row 0, which is false.document.form.name construct we've
been using, JavaScript also supports
document.form.elements[string]. This allows you to build
up the name as a string (e.g., "o0") in the middle of a program. The
checkAll function has an example. This will be useful
for seeing which rows are checked -- loop over the rows by number, and
check the state of the checkbox named "o"+row.Once we have a Boolean
formula, we can put values for the variables (e.g., A=true, B=false,
C=true) and evaluate the formula to see if the whole thing turns out
to be true or false. Fill in the missing code for
evaluateFormula to do that. The provided code already
extracts the formula and values from the form and puts the result into
the form. Your job is to make the result correct.
The particular structure of formula we're using here -- an OR of groups of ANDs of variables and their negations -- makes it relatively straightforward to evaluate a formula. The whole result starts off as false, and becomes true if any (OR) of the expressions for the rows is true. The partial result for a row starts off as true, and stays true as long as all (AND) the variables have the "right" values. A variable has the "right" value if it is true and shows up as itself in the expression (e.g., A=true for A*~B*C), or if it is false and shows up with a NOT sign in the expression (e.g., B=false for A*~B*C).
With this in mind, the algorithm marches down the string, character by character. It keeps a partial result for the AND that makes up a single row, and an overall result for the whole formula. For each character, it does a different action based on what that character is:
Note that in JavaScript, AND is written &&,
OR is written || and NOT is written !.
For extra credit (only do this once the whole rest of the homework is in great shape, and only if you want to), generalize the code to handle 4 or more variables (you'll need to use document.write to create the table), simplify the formula, etc.
A*~B*C + ~A*B*~C + A*~B*~C. What is its value if A=true,
B=false, C=false? What if A=false, B=true, C=true?XOR (exclusive or -- A or B but not both):
| A | B | out |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
NAND (not both A and B):
| A | B | out |
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Get a positive integer x
Get a positive integer y
Set z to 0
While y ≠ 0 do
Add x to z
Subtract 1 from y
End of loop
Output z
Halt