You are welcome to discuss these questions with your classmates, the instructor, or a TA, but I encourage you to try to find answers on your own before you seek help from others. If you make use of any outside information in answering these questions, or if you receive help from anyone, you need to acknowledge that along with your answer. Answers should be prepared with a word processor, printed out, and submitted at the beginning of class.
var x = 4; var y = 1; var z = 14; function fun1(x, y) { var z = x - y; return z; } function fun2(b, c) { if (b > c) return fun1(b, c); else return fun1(c, b); } function solutions() { out.innerHTML = ""; printValues("A. ", fun1(x, y)); printValues("B. ", fun1(z, x)); printValues("C. ", z); printValues("D. ", fun2(x, y)); printValues("E. ", fun2(y, x)); z = fun2(x, z); printValues("F. ", z); } function printValues(letter, value) { out.innerHTML += letter + value + "; "; out.innerHTML += "x , y, z = " + x + " , " + y + " , " + z + "<br>"; }
<div id='out'></div>
element later in the same file. What do you expect would be the
contents of that element if a user clicks on a button that causes a
call to the solutions()
method? Notice that the
printValues(letter, value)
method prints the letter
and value passed to it as parameters, and also the current values
of the global x
, y
, and z
variables at the top of the code segment.
(It's OK to be wrong, just write down what you think the results will be. You will not be graded on this bullet.)A. ?; x, y, z = ?, ?, ?
B.
C.
D.
E.
F.
Use the button below to get the actual results; write them down.
For each result (A - F), explain why the results were what they were. In particular, be sure to state what functions were called, what the actual parameters were when each function was called, and what the formal parameters were when each function started to execute. (6pts)
For example,
fun1
was called with actual parameters [what
were the values?].
Inside fun1
,
formal parameter x
had value ?
and formal parameter y
had value ?.
The return value was ?.
Assuming that each of the image files (butterfly.gif, etc.) contain images of the indicated objects, answer the following questions (8pts):<html> <head> <title>HW Page</title> </head> <body id="body"> <table id="table0"> <tr> <td id="cell00"> <img id="img0" src="butterfly.gif"> </td> <td id="cell01"> <img id="img1" src="coffeeCup.gif"> </td> </tr> <tr> <td id="cell10"> <img id="img2" src="computer.gif"> </td> <td id="cell11"> <img id="img3" src="eraser.gif"> </td> </tr> </table> </body> </html>
<script type="text/javascript"> document.getElementById('cell00').src = "fish.gif"; </script>
Yes? No?
<script type="text/javascript"> document.getElementById('img0').src = "fish.gif"; </script>
Yes? No?
<script type="text/javascript"> document.getElementById('cell00').innerHTML = '<img src="fish.gif">'; </script>
Yes? No?
<script type="text/javascript"> var someHTML = document.getElementById('cell01').innerHTML; document.getElementById('cell10').innerHTML = someHTML; </script>
<script type="text/javascript"> document.getElementById('cell11') = document.getElementById('cell00').innerHTML; </script>
var a = [1, 2, "hello"]; //A. a = ? a[0] = a[1]; //B. a = ? a[1] = a[2]; //C. a = ? a[2] = a[0]; //D. a = ? var index = 0; var b = [1, 2, "hello"]; var tmp = b[index]; b[index] = b[index + 1] //E. b = ? index++; b[index] = b[index + 1] //F. b = ? index++; b[index] = tmp; //G. b = ?
A. a = | 1 | 2 | "hello" |
B. a = |
C. a = |
D. a = |
E. b = |
F. b = |
G. b = |