To Lecture Notes

IT 238 -- Feb 4, 2026

Review Exercises

  1. What is the hex color code for olive (dark yellow)?
    Answer: #808000 (max for red, max for green, zero for blue)
  2. What are the JavaScript primitive datatypes that we have been using?
    Answer: string  number  boolean  undefined
  3. 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
    
    
  4. 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";
        }
    }
    
    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 = 354.48592938;
    var nRounded = n.toFixed(2);
    // Answer: 354.49
    
  5. 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.
  6. Answer:
    <!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>
    

Review for Midterm

var vs. let

Functions as First Class Objects

Anonymous Functions

Arrow Notation for Functions

Math Class Methods

Use Strict

Primitive vs. Class Variables

Project 3