CSE 190 M, Spring 2011
Final Exam Key

1. HTML/CSS Tracing

expected output

2. HTML/CSS Coding

<div id="main">
    <div id="column1">
        <h1>Zen Garden</h1>

        <div id="slogan">
            <p>A demonstration of...</p>
        </div>

        <div id="enlightenment">
            <h3>The Road to Enlightenment</h3>
            <p>Littering a dark and dreary road lay...</p>
            <p>Today, we must clear the mind...</p>

            <div id="selectadesign">
                <h3>Select Design</h3>
                <ul><li><a href="">Under the Sea!</a> by Eric Stoltz</li>
                    <li><a href="">Make 'em Proud</a> by Michael McAghon and Scotty Reifsnyder</li>...</ul>
            </div>
        </div>
    </div>

    <div id="whatabout">
        <h3>So What is This About?</h3>
        <p>There is clearly a need for CSS to be taken seriously...</p>
        <p>CSS allows complete and total control over the style of a hypertext document.</p>

        <h3>Participation</h3>
        <ul>
            <li>Graphic artists only please. You are modifying this page...</li>
            <li>Zen editors modify the style sheet in any way you wish, but not the HTML.</li>
        </ul>
    </div>
</div>
body {
    background-image: url("heading.gif");
    background-repeat: no-repeat;
    color: #471C47;
    font-family: Verdana, sans-serif;
    font-size: 9pt;
}

#main   { margin-left: 130px; }
h1      { display: none; }
#slogan { font-style: italic; }

#selectadesign   { border-top: 3px solid #A690AF; }
#selectadesign a { font-weight: bold; }

#enlightenment {
    background-image: url("toad.png");
    background-repeat: no-repeat;
    background-position: right bottom;
    float: left;
    padding-right: 1em;
    width: 26em;
}

#whatabout {
    background-color: #A690AF;
    border: 2px solid #471C47;
    float: left;
    padding: 1em;
    width: 21em;
}

#whatabout h3 { text-align: right; }

3. JavaScript / DOM

window.onload = function() {                        // document.observe is okay
    $("blend").observe("click", blendClick);        // onclick is okay
};

function blendClick() {
    $("output").innerHTML = "";                     // .update() okay
    
    var name = $("yourname").value;                 // .innerHTML NOT okay  (-1)
    var x = 0;
    var y = 0;
    
    for (var i = 0; i < name.length; i++) {
        if ($("sequential").checked) {              // .checked == "checked" NOT okay (-1)
            x = x + 15;
            y = y + 15;
        } else {
            x = parseInt(Math.random() * 300);      // Math.floor is okay
            y = parseInt(Math.random() * 100);      // 301, 101 okay
        }
        var span = document.createElement("span");  // new Element("span") okay
        span.innerHTML = name[i];                   // name.charAt(i) is okay;  .update() okay
        span.style.fontFamily = $("font").value;    // setting on whole output div is okay
        span.style.position = "absolute";
        span.style.left = x + "px";
        span.style.top  = y + "px";                 // setStyle({a:b, c:d, ...}) okay
        $("output").appendChild(span);
    }
}

4. PHP

<?php
# ok for letter to preg_match [A-Za-z]
# ok for times  to preg_match [0-9]+;  -1 if times matches just [0-9] or [0-9]*

if (!isset($_REQUEST["letter"]) || !isset($_REQUEST["times"]) ||
        strlen($_REQUEST["letter"]) != 1 || $_REQUEST["times"] <= 0) {
    header("HTTP/1.1 400 Invalid Request");
    die("invalid request");
}

$letter = strtolower($_REQUEST["letter"]);
$times = (int) $_REQUEST["times"];                               # cast to (int) is optional
$matches = 0;
foreach (file("peeps.txt", FILE_IGNORE_NEW_LINES) as $line) {    # file_get_contents/explode ok
    $lowerline = strtolower($line);                              # (explode on letter, check array length) ok
    $count = 0;
    for ($i = 0; $i < strlen($line); $i++) {                     # count($line) ok
        $ch = $lowerline[$i];
        if ($ch == $letter) {                                    # strcmp == 0 okay
            $count++;
        }
    }
    if ($count >= $times) {
        $matches++;
        ?> <p><strong><?= $line ?></strong>
              contains '<?= $letter ?>' exactly <?= $times ?> times.</p>
        <?php
    }
}

if ($matches == 0) {
    ?> <p>No names contained '<?= $letter ?>' enough times.</p>
    <?php
}
?>

5. SQL

-- all the times a director has appeared in his own movie, twice --
SELECT DISTINCT d.first_name, d.last_name
FROM directors d
JOIN movies_directors md ON md.director_id = d.id
JOIN roles r1 ON r1.movie_id = md.movie_id
JOIN roles r2 ON r2.movie_id = md.movie_id
JOIN actors a ON a.id = r1.actor_id AND a.id = r2.actor_id
WHERE a.first_name = d.first_name AND a.last_name = d.last_name
  AND r1.role < r2.role
ORDER BY d.last_name, d.first_name;