// CSE 154, Spring 2013
// JavaScript code for Homework Lateness example
//
// This code fetches and processes XML data from the following URL:
// https://webster.cs.washington.edu/cse154/hw/hw.php
window.onload = function() {
document.getElementById("fetch").onclick = fetchData;
}
// Called when the Fetch button is clicked.
// Looks up XML data
function fetchData() {
var hw = parseInt(document.getElementById("assignment").value);
var ajax = new XMLHttpRequest();
ajax.onload = whyULate;
ajax.open("GET", "https://webster.cs.washington.edu/cse154/hw/hw.php?assignment=hw"+hw, true);
ajax.send();
}
// Process returned XML data to calculate how many students were late.
function whyULate() {
var latepeeps = 0;
console.log(this.responseXML);
var submissions = this.responseXML.querySelectorAll("submission date");
for (var i = 0; i < submissions.length; i++) {
var late = parseInt(submissions[i].getAttribute("lateness"));
if (late) {
latepeeps++;
}
}
alert("There were " + latepeeps + " students late");
}
// Example XML data:
// assignment name="hw1">
//
//
// Wed Apr 8
//
//
//
//
//
// Sat Apr 11
//
//
//
//
// Mon Apr 6
//
//
// ...
//
//