To Notes

IT 130 -- 10/28/08

 

Review Questions

  1. What are the advantages of using the document.getElementById function over using fully qualified control names?

    Ans: No form is needed; a variable can be used for the ID.

  2. Convert this tag so it can be used with getElementById:

    <input type="text" name="txtInput" size="20" />
    
    Ans:

    <input type="text" id="txtInput" size="20" />
    
  3. Convert this JavaScript function so that is uses getElementById:

    function milesToCm( )
    {
        var m, c;
        m = parseFloat(document.frmConvert.txtMiles.value);
        c = 5280 * 12 * 2.54 * m;
        document.frmConvert.txtCm.value = c;
    }
    
    Ans:

    function milesToCm( )
    {
        var m, c;
        m = parseFloat(document.getElementById("txtMiles").value);
        c = 5280 * 12 * 2.54 * m;
        document.getElementById("txtCm").value = c;
    }
    
  4. For what are div tags used?

    Ans: For our uses, div tags allow absolute positioning of text or images. This is useful if you want to place images or text on top of each other.

  5. What is abstraction and how does it apply to functions?

    Ans:Abstraction means ignoring irrelavant details and concentrating on essential ones. To use a builtin function, you don't need to know how the code works, you only need to know what it expects as input and what it does or produces as output.

  6. Why are functions useful?

    Ans: (1) Abstraction, (2) Code Reuse or Avoid Code Duplication, (3) Event handlers.

  7. What is the output?

    <html>
    <head>
    <script type="text/javascript">
    function f(x)
    {
        document.writeln(2 * x + 1);
    }
    </script>
    </head>
    <body>
    <script type="text/javascript">
    f(5);
    </script>
    </body>
    </html>
    
    Ans: 11

  8. Name as many builtin Math functions as you can.

    Note: Math.floor together with Math.random will be useful for generating random integers.

    Ans: Math.sqrt, Math.random, Math.pow are the ones that we have discussed. Math.round is also available. The trig functions Math.sin, Math.cos, and Math.tan are also available if you remember your trigonometry from highschool.

  9. Find the mistakes in the following event handler that displays random outputs from a six-sided die:

    Ans: Here is the fixed version:

  10. Draw the variable trace table and predict the output.
    var a = "dog", b = "cat", c = "mouse";
    d = c;
    c = b;
    b = a;
    a = d;
    document.writeln(a + " " + b + " " + c);
    
    Ans:
    a b c d
    "dog" "cat" "mouse"  
    "mouse" "dog" "cat" "mouse"

    Output: mouse dog cat

 

A Color Matching Game

 

Dynamically Displayed Images

 

The Builtin Function Math.floor

 

Generating Random Integers

 

The Dice Examples

 

 

Project Proj5