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.
    
    function fun1(b, c)
    {
      if (b > c)
        return fun2(b, c);
      else
        return fun2(c, b);
    }
    
    function fun2(x, y)
    {
      var z = x - y;
      return z;
    }
    
    var x = 4;
    var y = 1;
    var z = 14;
    
    alert( "A. " + fun2(x, y) );    // A. ???
    alert( "B. " + fun2(z, x) );    // B. ???
    alert( "C. " + z );             // C. ???
    
    alert( "D. " + fun1(x, y) );    // D. ???
    alert( "E. " + fun1(y, x) );    // E. ???
    
    z = fun1(x, z);
    alert( "F. " + z );             // F. ???
    
    
  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 src="butterfly.gif">  </td> 
    <td id="cell01"> <img src="coffeeCup.gif">  </td> 
    </tr>
    <tr>
    <td id="cell10"> <img src="computer.gif">  </td> 
    <td id="cell11"> <img 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>
        
      2. <script type="text/javascript">
        document.getElementById('img0').src = "fish.gif";
        </script>
        
      3. <script type="text/javascript">
        document.getElementById('cell00').innerHTML = '<img src="fish.gif">';
        </script>
                
    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 =