University of Washington, CSE 154
Lab 10: Practice Final Exam Problems, ANSWER KEY

1. HTML/CSS Interpreting


What a great page!!

I never saw a purple cow, I never hope to see one. But I can tell you anyhow I'd rather see than be one.

Copyright 2007
CSE.

This
is
the
end!

What about me?

2. JavaScript/DOM

window.onload = function() {
    var tables = document.querySelectorAll("table");
    for (var table = 0; table < tables.length; table++){
        var rows = tables[table].getElementsByTagName("tr");
        for (var row = 0; row < rows.length; row++) {
            var tds = rows[row].getElementsByTagName("td");
            for (var col = 0; col < tds.length; col++) {
                if (row % 2 == col % 2) {
                    tds[col].classList.add("chess");
                }
            }
        }
    }
};

3. Ajax/XML

window.onload = function() {
    var ajax = new XMLHttpRequest();
    ajax.onload = createRects;
    ajax.open("GET", "rect.php", true);
    ajax.send();
};

function createRects() {
    var rects = this.responseXML.getElementsByTagName("rectangle");
    for (var i = 0; i < rects.length; i++) {
        var div = document.createElement("div");
        div.style.position = "absolute";
        div.style.border = "2px solid black";
        div.style.width = rects[i].getAttribute("width") + "px";
        div.style.height = rects[i].getAttribute("height") + "px";
        div.style.left = rects[i].getAttribute("x") + "px";
        div.style.top = rects[i].getAttribute("y") + "px";
        div.style.backgroundColor = "#" + rects[i].getAttribute("color");
        document.getElementById("rectanglearea").appendChild(div);
    }
}

4. PHP

<?php
header("Content-Type: text/plain");
$search_name = $_GET["name"];
$found = FALSE;

$text = file_get_contents("employees.txt");
$lines = explode("\n", $text);
foreach ($lines as $line) {
    $tokens = preg_split("/\t+/", $line);
    if ($tokens[0] == $search_name) {
        $found = TRUE;
        $title = preg_replace("/[^a-zA-Z]+/", "", $tokens[2]);
        print "http://www.awesomeco.com/$title/{$tokens[1]}";
    }
}

if (!$found) {
    print "http://www.awesomeco.com/";
}
?>

5. SQL

SELECT   a1.first_name, a2.first_name, a2.last_name, m.name
FROM     movies m
JOIN     roles r1 ON r1.movie_id = m.id
JOIN     actors a1 ON a1.id = r1.actor_id
JOIN     roles r2 ON m.id = r2.movie_id
JOIN     actors a2 ON a2.id = r2.actor_id
WHERE    a1.last_name = a2.last_name
AND      a1.first_name < a2.first_name
ORDER BY a2.last_name;

6. PHP+HTML+SQL

function display_last_names() {
    # connect to database
    $db = new PDO("mysql:dbname=imdb", "username", "password");
    $query = "SELECT   a1.first_name AS first1, a2.first_name AS first2, a2.last_name AS last, m.name AS movie
              FROM     movies m
              JOIN     roles r1 ON r1.movie_id = m.id
              JOIN     actors a1 ON a1.id = r1.actor_id
              JOIN     roles r2 ON m.id = r2.movie_id
              JOIN     actors a2 ON a2.id = r2.actor_id
              WHERE    a1.last_name = a2.last_name
              AND      a1.first_name < a2.first_name
              ORDER BY last;";
    $rows = $db->query($query);
    ?>

    <ol>

        <?php
        $second = FALSE;
        foreach ($rows as $row) {
            if (!$second && preg_match("/^[K-Z]/", $row["last"])) {
                # end first list and start second
                $second = TRUE;
                ?>

                </ol>
                <ol>

                <?php
            }
            ?>
            
            <li>
                <?= $row["last"] ?>, <?= $row["first1"] ?> /
                <?= $row["first2"] ?>: <?= $row["movie"] ?>
            </li>

            <?php
        }
        ?>

    </ol>

<?php
}