COMP 105: HW #3


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.


  1. Consider the following JavaScript code segment.
    
    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>";
    }
    
    
  2. Consider the following simple web page:
    
    <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>
    
    
    Assuming that each of the image files (butterfly.gif, etc.) contain images of the indicated objects, answer the following questions (8pts):
    1. Briefly describe how this page will appear when it is rendered by a web-browser: Where will the images be relative to each other? Will the page contain any text?
       
    2. Which of the following code fragments will correctly replace the image in the upper-left with an image of a fish?
      1. <script type="text/javascript">
        document.getElementById('cell00').src = "fish.gif";
        </script>
        
        Yes?   No?
      2. <script type="text/javascript">
        document.getElementById('img0').src = "fish.gif";
        </script>
        
        Yes?   No?
      3. <script type="text/javascript">
        document.getElementById('cell00').innerHTML = '<img src="fish.gif">';
        </script>
        
        Yes?   No?
    3. Assume that the following script is added to the page below the table. Will this script result in an error? If so, why? If not, how will it change the appearance of the page?
      <script type="text/javascript">
      var someHTML = document.getElementById('cell01').innerHTML;
      document.getElementById('cell10').innerHTML = someHTML;
      </script>
      
       
    4. Assume that the following script is added to the page below the table. Will this script result in an error? If so, why? If not, how will it change the appearance of the page?
      <script type="text/javascript">
      document.getElementById('cell11') = document.getElementById('cell00').innerHTML;
      </script>
      
       
  3. For each of the statements indicated below, draw boxes illustrating the contents of the array after the corresponding line of JavaScript has executed. The first one is done for you. (6pts)
    
        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 =