To Lecture Notes

IT 238 -- Apr 27, 2026

Review Exercises

  1. What is wrong with the beginning of this HTML file?
    <!-- Stan Smith
         Project 3
         Feb 6, 2021 -->
    
    <!DOCTYPE html>
    <html lang="en">
    ....
    
    Answer: The first line of the HTML page should be
    <!DOCTYPE html>
    Source code comments or other code should go after that. Here is what the beginning of the HTML file should look like:
    <!DOCTYPE html>
    
    <!-- Stan Smith
         Project 3
         May 1, 2026 -->
    
    <html lang="en">
    
  2. Arrange these operators in order from highest precedence to lowest precedence:
    +  ==  =  ++  <  **  +=  .  *  &&
    Answer:
    .  ++  **  *  +  <  ==  &&  =  +=
    
    Look at the W3Schools Operator Precedence Table
  3. What is the value of the array a after these two statements?
    var a = ["apple", "peach", "cherry", "orange", "lemon", "pear"];
    a.splice(1, 3, "carrot", "celery"];
    // Answer:
    ["apple", "carrot", "celery", "lemon", "pear"]
    
  4. Look up three of these Math class methods on the W3Schools site and test them:
    abs  cos  max  min  pow  sin  sqrt
    // Answer:
    document.writeln(Math.abs(-34.72) + " " + Math.abs(84.38) + "<br>");
    document.writeln(Math.sin(0) + "<br>");
    document.writeln(Math.sqrt(2));
    // Output:
    34.72 84.38
    0
    1.41421356237309505
    
  5. How do you write a function in arrow notation? Answer:
  6. function greeting( ) {
        return "Hello, World.";
    }
    // is written in arrow notation as
    var greeting = ( ) => "Hello, World.";
    
  7. Convert this function to arrow notation:
    function convert( ) {
        var input1 = document.getElementById("tf1");
        var cm = parseFloat(input1.value) * 2.54;
        var output1 = document.getElementById("para1");
        output1.innerHTML = `Length in cm: ${output1}`);
    }
    // Answer:
    var convert = ( ) => {
        var input1 = document.getElementById("tf1");
        var cm = parseFloat(input1.value) * 2.54;
        var output1 = document.getElementById("para1");
        output1.innerHTML = `Length in cm: ${output1}`);
    }
    
    Then pass this function to this JS statement as an anonymous function:
    button1.addEventListener("click", convert);
    
    Answer:
    button1.addEventListener("click", ( ) => {
        var input1 = document.getElementById("tf1");
        var cm = parseFloat(input1.value) * 2.54;
        var output1 = document.getElementById("para1");
        output1.innerHTML = `Length in cm: ${output1}`);
    });
    
  8. Write an HTML page with a button and a script file that changes the background color of the document to red when the button is clicked. Use anonymous functions in arrow notation.
    Answer: Place a button in the body:
    <button id="btn">Click Me</button>
    
    You can also use this input tag:
    <input type="button" id="btn" value="Click Me">
    
    Then use this script.js file:
    function changeColor( ) {
        var bodyObject = document.getElementById("b");
        bodyObject.style.backgroundColor = "red";
    }
    function init( ) {
        document.getElementById("btn").
            addEventListener("click", changeColor);
    }
    window.addEventListener("load", init);
    
    The preceding lines can be written using anonymous methods for init and changeColor:
    window.addEventListener("load", ( ) => {
        var bodyObject = document.getElementById("btn");
        bodyObject.addEventListener("click", ( ) => {
            document.getElementById("btn")
                .bodyObject.style.backgroundColor = "red";
        });
    }); 
    
  9. In an <img> element in the body, when the window is loaded or refreshed, randomly display one of these images: heads.png or tails.png.
  10. Write an object literal 24-hour clock object with properties hr, min, sec, and methods tick (add one to sec), toString that outputs a time like this: 15:07:49. Answer:
    var clock = { hr: 23;
                  min: 59;
                  sec: 59;
                  tick: function( ) {
                       this.sec;
                       if (this.sec == 60) {
                           this.min++;
                           this.sec = 0;
                       }
                       if (this.min == 60) {
                           this.hr++;
                           this.min = 0;
                       }
                       if (this.hr == 60) {
                           this.hr = 0;
                       }
                       toString: function( ) {
                           return `${hr} ${hr} ${hr}`;
                  }
    document.writeln(clock + "<br>");
    clock.tick( );
    document.writeln(clock + "<br>");
    

Primitive vs. Class Variables

Use Strict

HTML Controls

HTML Events

blur  change  click  dblclick  focus  keydown  keypress  keyup  load
mousedown  mouseenter  mouseleave  mousemove  mouseup

HTML Events (W3Schools)