To Lecture Notes

IT 238 -- Jun 1, 2026

Practice Exercises

  1. We know about HTML entities, which are special characters whose HTML name starts with an ampersand (&) and ends with a semicolon (;).  Some examples are
       <  >  "  '  &  
    
    What is a JavaScript escape character? Give some examples of such escape sequences.
    Answer: The Java escape character is the backslash character \, which changes the meaning of the character after it. The combination of \ with the next character after it is called an escape sequence. Here are some JavaScript escape sequences:
    \n -- new line
    \t -- tab
    \' -- literal single quote character
    \" -- literal double quote character
    \\ -- literal backspace character
    
    The \n character works in a textarea element, but it does not work in a paragraph element. This is because new lines are eliminated by the browser when text is displayed in a paragraph. To actually go to a new line in a paragraph, insert a <br> element instead. The newline character \n is used to go to a new line in an alert box or in a textarea:
    function init( ) {
        alert("This is a test line.\nThis is the second line.")
        var taObj = document.querySelector("textarea");
        taObj.value = "This is a test line.\nThis is the second line."
    }
    
  2. What does the String method charAt do? Give an example.
    Answer: charAt returns the character specified by the zero-based index. For example:
    var s = "elephant";
    document.writeln(s.charAt(4));
    // Output: h
    
  3. For an input element, what are some values of the type attribute that we have seen? Answer:
    text -- a normal textfield
    number -- a textfield that can contain only numbers
    radio -- radio button
    checkbox -- check box
    file -- file download element
    range -- slider, also called a trackbar
    color -- color picker
    date -- date picker or calendar
    password -- textfield that hides the characters that are entered
  4. Show how to use a datepicker element (input element with type="date"). Answer:
    <!DOCTYPE html>
    <!-- DatePicker Example -- Source code file: index.html -->
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>DatePicker Example</title>
            <script>
            function displayDate( ) {
                var datePicker = document.querySelector("input");
                var para = document.querySelector("p");
                para.textContent = datePicker.value;
            }
            function init( ) {
                var datePicker = document.querySelector("input");
                datePicker.addEventListener("change", displayDate);
            }
            document.addEventListener("DOMContentLoaded", init);
            </script>
        </head>
        <body>
            <h1>DatePicker Example</h1>
            <input type="date" name="date">
            <p></p>
        </body>
    </html>
    
  5. Look at these InputElement Examples:
    Required
    Web Page  Source Code
    Slider
    Web Page  Source Code
    ColorPicker
    Web Page  Source Code
    ImagePreview
    Web Page  Source Code
  6. Implement an input form with fields name, gender, and age. Validate the fields, checking that name is non-empty, gender is one of the values F, M, or X, and that age is non-negative. Use an input element with type="number" for age. Use error messages (color red) on the form to indicate the fields that do not pass validation. For example:



    Name cannot be empty.



    Age must be F, M, or X.



    Age must be non-negative.

    Answer:
    ===========================================
    <!DOCTYPE html>
    
    <!-- Validation2 Example
    Source code file: index.html -->
    
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Document</title>
            <link rel="stylesheet" href="styles.css">
            <script src="script.js"></script>
        </head>
        <body>
            <h1>Kid Info Submission Form</h1>
            <form id="form1" name="form1"
                  action="http:ectweb.cs.depaul.edu/sjost/receipt.php"
                  method="POST">
    
                <label>Name:</label><br>
                <input type="text" id="fname" name="fname"><br>
                <span class="err" id="fname-err">
                    Name cannot be empty.<br>
                </span><br>
    
                <label>Gender:</label><br>
                <input type="text" id="gender" name="gender"><br>
                <span class="err" id="gender-err">
                    Gender must be F, M, or X.<br>
                </span><br>
    
                <label>Age:</label><br>
                <input type="number" id="age" name="age" value="0"><br>
                <span class="err" id="age-err">
                    Age must be non-negative.<br> 
                </span><br><br> 
    
                <input type="submit" value="Submit Form">
            </form>
        </body>
    </html>
    -------------------------------------------
    /* Validation2 Example
       Source code file: styles.css */
    body { font: 100% Verdana, Arial, sans-serif; 
           color: navy; background-color: #E0E0FF; }
    .err { color: red; display : none; }
    -------------------------------------------
    // Validation2 Example
    // Source code file: script.js
    
    // Event listener to validate form when
    // it is submitted.
    function validateForm(e) {
    
        // Validate fname.
        var fnameObj = document.querySelector("#fname");
        fnameErr = document.querySelector("#fname-err");
        var fnameOK;
        if (fnameObj.value != "") {
            fnameOK = true;
            fnameErr.style.display = "none";
        }
        else {
            fnameOK = false;
            fnameErr.style.display = "block";
        }
    
        // Validate gender.
        var genderObj = document.querySelector("#gender");
        var genderErr = document.querySelector("#gender-err");
        var genderOK;
        if (genderObj.value != "M" && genderObj.value != "F" &&
                genderObj.value != "X") {
            genderOK = false;
            genderErr.style.display = "block";
        }
        else {
            genderOK = true;
            genderErr.style.display = "none";
        }
    
        // Validate age.
        var ageObj = document.querySelector("#age");
        var ageErr = document.querySelector("#age-err");
        var ageOK;
        if (parseInt(ageObj.value) < 0) {
            ageOK = false;
            ageErr.style.display = "block";
        }
        else {
            ageOK = true;
            ageErr.style.display = "none";
        }
    
        // Cancel submission if at least one
        // field is not validated.
        if (fnameOK == false | 
            genderOK == false | 
            ageOK == false) {
                e.preventDefault( );
        }
    }
    
    // Attach validation event listener to form.
    function init( ) {
        var formObj = document.querySelector("#form1");
        formObj.addEventListener("submit", validateForm);
    }
    
    document.addEventListener("DOMContentLoaded", init);
    ===========================================
    

Review Items on ExamInfo Page

Two Dimensional Arrays

Conway's Game of Life