To Lecture Notes

IT 238 -- Apr 22, 2026

Review Exercises

  1. Give some examples of composite datatypes.
    Answer: (a) array, (b) function, (c) any user defined object literal. The typeof operator returns object for a and c. It returns function as the datatype of for any function or method. Here is JS code to test this:
    // Create an array a
    var a = [4, 7, 3, 5];
    // Define the function s
    function s(n) {
        return n ** 2;
    }
    // Define object literal:
    var kid = { name: "Alice", age: 11 };
    
    // Check that typeof gives object for arrays and
    // object literals.  It gives function as the datatype
    // of a function 
    document.writeln(typeof a);
    document.writeln(typeof b);
    document.writeln(typeof kid);
    // Output: object function object
    
    
  2. Rewrite this traditional for loop as a modern for loop:
    var output = "";
    for(var i = 0; i < kidsArray.length; i++) {
        if (kidsArray[i].age < 12) {
            output += kidsArray[i].name + "\n";
        }
    }
    
    Answer:
    var output = "";
    for(var kid of kidsArray) {
        if (kid.age < 12) {
            output += kid.name + "\n";
        }
    }
    
  3. We saw earlier that the following lines round the value of n to two digits after the decimal point:
    var n = 34.83728;
    var nRounded = Math.round(n * 100) / 100;
    
    Verify that the following line also rounds n to two digits after the decimal point:
    var n = 34.83728;
    var nRounded = n.toFixed(2);
    
    Answer: both methods of rounding give the answer 34.84.
  4. We know how to attach a click event listener f to a button object btn1:
    btn1.addEventListener("click", f);
    
    Show how to use the onclick property to add the event listener f to the button object btn1. Answer:
    btn1.onclick = f;
    
    Do the same thing for a load event listener.
    Answer:
    window.addEventListener("load", init);
    can also be written as
    window.onload = init;
    
    Here is the complete source code:
    <!DOCTYPE html>
    <html lang="en">
        <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Test Button</title>
            <script>
            function f( ) {
                var output = document.getElementById("para1");
                output.innerHTML = "Button test succeeded.";
            }
            function init( ) {
                var btn1 = document.getElementById("button1");
                // btn1.addEventListener("click", f);
                // or
                btn1.onclick = f;
            }
            // window.addEventListener("load", init);
            // or
            window.onload = init;
            </script>
        </head>
        <body>
            <h1>Test Button</h1>
            <button id="button1">Click Me to Test Button</button>
            <p id="para1" />
        </body>
    </html>
    
  5. Look at the solution of Review Exercise 14 from the April 20 Notes.

var vs. let

Functions as First Class Objects

Anonymous Functions

Arrow Notation for Functions