To Lecture Notes

IT 238 -- June 2, 2025

Practice Exercises

  1. 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 = "rhinoceros";
    document.writeln(s.charAt(5));
    // Output: c
    
  2. Look at these InputElement Examples:
    Required
    Web Page  Source Code
    Slider
    Web Page  Source Code
    ColorPicker
    Web Page  Source Code
  3. Place a select element on an HTML page that displays the three state names Alabama, Alaska, Arizona.
  4. Insert all fifty state names taken from this array into a dropdown box:
    ["Alabama", "Alaska", "Arizona", "Arkansas", 
    "California", "Colorado", "Connecticut", 
    "Delaware", "Florida", "Georgia", "Hawaii", 
    "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", 
    "Kentucky", "Louisiana", "Maine", "Maryland", 
    "Massachusetts", "Michigan", "Minnesota", 
    "Mississippi", "Missouri", "Montana", "Nebraska",
    "Nevada", "New Hampshire", "New Jersey", 
    "New Mexico", "New York", "North Carolina", 
    "North Dakota", "Ohio", "Oklahoma", "Oregon",
    "Pennsylvania", "Rhode Island", "South Carolina", 
    "South Dakota", "Tennessee", "Texas", "Utah", 
    "Vermont", "Virginia", "Washington", 
    "West Virginia", "Wisconsin", "Wyoming"]
    
    Answer:
    <!DOCTYPE html>
    <html>
        <head>
            <script>
            
            function init( ) {
                var states = ["Alabama", "Alaska", "Arizona", "Arkansas", 
                    "California", "Colorado", "Connecticut", "Delaware", 
                    "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", 
                    "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", 
                    "Maine", "Maryland", "Massachusetts", "Michigan", 
                    "Minnesota", "Mississippi", "Missouri", "Montana", 
                    "Nebraska","Nevada", "New Hampshire", "New Jersey", 
                    "New Mexico", "New York", "North Carolina", 
                    "North Dakota", "Ohio", "Oklahoma", "Oregon",
                    "Pennsylvania", "Rhode Island", "South Carolina", 
                    "South Dakota", "Tennessee", "Texas", "Utah", 
                    "Vermont", "Virginia", "Washington", 
                    "West Virginia", "Wisconsin", "Wyoming"];
    
                var selectTag = document.querySelector("select");
                for(let state of states) {
                    var optionTag = document.createElement("option");
                    optionTag.value = state;
                    optionTag.innerHTML = state;
                    selectTag.appendChild(optionTag);
                }
                selectTag.addEventListener("change", showState);
            }
            document.setEventListener("DOMContentLoaded", init);
            </script>
        </head>
        <body>
            <h1>Dropdown List of States</h1>
            <select></select><br><br>
            <p></p>
        </body>
    </html>
    
  5. Add a button to your page of Exercise 4 to display the selected state name in a paragraph when the button is clicked. Answer: add this code at the end of the init method of Exercise
    var button1 = document.querySelector("button");
    button1.onclick = showSelectedState;
    
    Then add this event handler to the script file:
    function showSelectedState( ) {
        var select1 = document.querySelector("select");
        var p1 = document.querySelector("p");
        p1.innerHTML = "Selected State: " + select1.value;
    }
    
  6. Remove the button from Exercise 5 and display the selected state name when the selection is changed. Change the two statements at the end of the init method to
    selectTag.onchange = showSelectedState;
    
  7. 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