CSE 190 M, Spring 2009
Final Exam Key

1. HTML/CSS Tracing

q1 key

2. PHP

<?php
if (isset($_REQUEST["student"])) {
    $student = $_REQUEST["student"];
    $filename = "students/$student/asciimation.txt";
    if (file_exists($filename)) {
        $text = file_get_contents($filename);
        $frames = explode("=====\n", $text);
        $frame = 0;
        if (isset($_REQUEST["frame"])) {
            $frame = (int) $_REQUEST["frame"];
        }
        
        if ($frame < count($frames)) {
            header("Content-type: text/plain");
            print $frames[$frame];
        }
    }
}
?>

3. JavaScript/DOM

var answer;
var guesses = 0;

document.observe("dom:loaded", function() {
    $("makeguess").observe("click", makeGuessClick);
    answer = parseInt(Math.random() * 100) + 1;
});

function makeGuessClick() {
    guesses++;
    var guess = $("number").value;
    var li = document.createElement("li");
    li.innerHTML = guess;

    if (guess > answer) {
        $("result").innerHTML = "Too high!";
        li.addClassName("high");
    } else if (guess < answer) {
        $("result").innerHTML = "Too low!";
        li.addClassName("low");
    } else {
        $("result").innerHTML = "You got it right in " + guesses + " tries!";
        $("makeguess").disabled = true;
    }

    $("guesses").appendChild(li);
}

4. Ajax/XML

document.observe("dom:loaded", function() {
    new Ajax.Request("critter.php", {
        method: "get",
        onSuccess: ajaxSuccess
    });
});

function ajaxSuccess(ajax) {
    var moves = ajax.responseXML.getElementsByTagName("move");
    for (var i = 0; i < moves.length; i++) {
        var animal = moves[i].getAttribute("animal");
        var move = moves[i].firstChild.nodeValue;

        var critters = $$("." + animal);
        for (var j = 0; j < critters.length; j++) {
            var x = parseInt(critters[j].getStyle("left"));
            var y = parseInt(critters[j].getStyle("top"));
            if (move == "N")      { y -= 20; }
            else if (move == "S") { y += 20; }
            else if (move == "W") { x -= 20; }
            else if (move == "E") { x += 20; }
            critters[j].style.left = x + "px";
            critters[j].style.top = y + "px";
        }
    }
}

5. SQL

SELECT DISTINCT a.first_name, a.last_name
FROM actors a
     JOIN roles r1 ON r1.actor_id = a.id
     JOIN movies m ON m.id = r1.movie_id
     JOIN movies_genres mg ON mg.movie_id = m.id
     JOIN roles r2 ON r2.actor_id = a.id
     JOIN roles r3 ON r3.actor_id = a.id
WHERE r1.movie_id <> r2.movie_id
     AND r1.movie_id <> r3.movie_id
     AND r2.movie_id <> r3.movie_id 
     AND mg.genre = 'Action'
ORDER BY a.last_name, a.first_name;