To Lecture Notes

IT 238 -- May 19, 2025

Practice Exercises

  1. How does the Elvis operator work?
    Answer: The Elvis operator is so named because a ? rotated 90 degrees counterclockwise looks like Elvis Presley's wavy hair. This operator is also called the tertiary conditional operator because it accepts three operands: (1) condition, (2) result if condition is true, and (3) result if condition is false:
    var result = condition ? resultIfTrue : resultIfFalse;
    
    This is just a one-line if-else statement:
    var result;
    if (condition) {
        result = resultIfTrue;
    }
    else {
        result = resultIfFalse;
    }
    
    For example:
    var x = 259;
    var size = (x >= 100) ? "Large" : "Small";
    
  2. Write a script that shows a green check or red X image when a check box element is checked or unchecked?
    Initially, the images are empty with background color #C0C0C0. Answer:
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Exercise 2</title>
            <style>
            #img1, #img2, #img3 { 
                display: inline;
                width: 15px; height: 15px;
                background-color: #C0C0C0; }
            </style>
            <script>
            function showIcon(e) {
                var cbObj = e.target; 
                var cbIndex = cbObj.id.charAt(2);
                var imgObj = document.querySelector("#img" + cbIndex);
                var imageFile = cbObj.checked ? 
                    "green-check.png" : "red-x.png";
                imgObj.src = imageFile;
            }
    
            function init( ) {
                for(var i = 1; i <= 3; i++) {
                    document.querySelector("#cb" + i).
                    addEventListener("change", showIcon);
                }
            }
            window.addEventListener("load", init);
            </script>
        </head>
        <body>
            <h1>Exercise 2</h1>
            <ol>
                <li><img id="img1">
                <input type="checkbox" id="cb1">
                <label for="cb1">Checkbox 1</label></li>
    
                <li><img id="img2">
                <input type="checkbox" id="cb2">
                <label for="cb2">Checkbox 12</label></li>
    
                <li><img id="img3">
                <input type="checkbox" id="cb3">
                <label for="cb3">Checkbox 3</label></li>
            </ol>
        </body>
    </html>
    
  3. For Project 4, how do you set up the hints to be initially hidden?
  4. In jQuery, the statement
    $(document).ready(init);
    
    is usually written as its shorter version
    $(init);
    
    Rewrite this script as its shorter version and test it:
    $(document).ready( ( ) => {
        $("#btn1").click( ( ) => {
            $("#p1").text("Hello, World!");
        });
    });
    
    Answer: here is the entire HTML file with the revised script.
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>Exercise 3</title>
            <script>
            $( ( ) => {
                $("#btn1").click( ( ) => {
                    $("#p1").text("Hello, World!");
                });
            });
            </script>
        </head>
        <body>
            <h1>Exercise 3</h1>
            <button id="btn1">Show Greeting</button>
            <p id="p1"></p>
        </body>
    </html>
    
  5. Test the following jQuery script that dynamically adds content to paragraph like this:

    Halloween Greeting Example
    Happy Halloween

    $( ( ) => {
        $("#p1").html("Halloween Greeting Example<br>Happy Halloween");
        $("#p1").css("font", "bold 200% Chiller");
        $("#p1").css("backgroundColor", "#FF8000");
    });
    
    Translate this script to Vanilla JavaScript. Answer:
    window.addEventListener("load", ( ) => {
        var pObj = document.querySelector("#p1");
        pObj.innerHTML = "Halloween Greeting " +
            "Example<br>Happy Halloween";
        pObj.style.font = "bold 200% Chiller";
        pObj.style.backgroundColor = "#FF8000";
    });
    

Practice Quiz

Examples for Project 4

More jQuery Examples