Here we have code segments in ajax, javascript, html, and an xml file.
I'm looking to calculate the total score based on user input to determine if they selected the correct answers for a test. I've attempted to figure this out, but I'm struggling with developing the necessary code to solve this issue.
Any advice would be highly appreciated. Thank you for taking the time to read this.
This snippet includes javascript and ajax.
var score;
var i;
function loaddata() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
// r = xhr.JSONparse(responseText);
displayData(xhr);
}
};
xhr.open("GET", "FinalQuiz.xml", true);
xhr.send();
}
function displayData(xhr) {
console.log(xhr);
// Retrieving data as an xml file
var xmldoc = xhr.responseXML;
// Begin table
var output="";
// Process data by record
var x = xmldoc.getElementsByTagName("question");
for(i = 0; i<x.length; i++)
{
output+="Question ";
output+=x[i].getElementsByTagName("qnumber")[0].childNodes[0].nodeValue + " ";
output+="<br>";
output+="Title: ";
output+=x[i].getElementsByTagName("qtitle")[0].childNodes[0].nodeValue + " ";
output+="<br>";
output+= "<button onclick=\"answer()\">A</button>" ;
output+=x[i].getElementsByTagName("a")[0].childNodes[0].nodeValue + " ";
output+="<br>";
output+= "<button onclick=\"answer()\">B</button>" ;
output+=x[i].getElementsByTagName("b")[0].childNodes[0].nodeValue + " ";
output+="<br>";
output+= "<button onclick=\"answer()\">C</button>" ;
output+=x[i].getElementsByTagName("c")[0].childNodes[0].nodeValue + " ";
output+="<br>";
output+= "<button onclick=\"answer()\">D</button>" ;
output+=x[i].getElementsByTagName("d")[0].childNodes[0].nodeValue;
output+="<br>";
output+="<br>";
}
document.getElementById("quiz").innerHTML = output;
}
function answer()
{
score = score + 1;
}
The following is the xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE finalquiz SYSTEM "FinalQuiz.dtd" >
<finalquiz>
... [XML content here]
</finalquiz>
This corresponds to the html file.
[HTML content here]