To Lecture Notes

IT 238 -- Apr 30, 2025

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 must 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
         Feb 6, 2021 -->
    
    <html lang="en">
    
  2. 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:
    <input type="button" id="btn" value="Click Me">
    
    You can also use a button tag:
    <button id="btn">Click Me</button>
    
    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";
        });
    }); 
    
  3. 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. Answer:
    <!DOCTYPE html>
    <!-- Test Coin Flip Example:
         Source code file: test-coin-flip.html -->
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Test Coin Flip</title>
            <style>
                img { width: 250px; }
            </style>
            <script>
            function flipCoin( ) {
                var image1 = document.getElementById("coin");
                var randomValue = Math.random( );
                if (randomValue >= 0.5) {
                    image1.src = "heads.png";
                }
                else {
                    image1.src = "tails.png";
                }
            }
            window.addEventListener("load", flipCoin);
            </script>
        </head>
        <body>
            <h1>Test Coin Flip</h1>
            <img id="coin" alt="Heads or Tails">
        </body>
    </html>
    
  4. What is the difference between the Array methods slice and splice? Look up these methods in W3Schools. Try the methods out on this array:
    var a = [34, 72, 19, 5, 90, 7];
    

Primitive vs. Class Variables

Classes

Project 3

var vs. let

HTML Controls

HTML Events

blur  change  click  dblclick  focus  input  keydown  keypress  keyup  
load  mousedown  mouseenter  mouseleave  mousemove  mouseup  resize
scroll  select  submit  wheel

HTML Events (W3Schools)