Challenge:
Attempting to display one question and its four corresponding answers at a time from an XML file.
JavaScript code:
var xmlDoc, quest, ans, i, n;
xmlDoc = loadXMLDoc("questions.xml");
quest = xmlDoc.getElementsByTagName('main');
document.write("<table border='1'>");
for (i = 0; i < quest.length; i+=1)
{
document.write("<tr><td>");
document.write( quest[i].childNodes[0].nodeValue );
document.write("</td></tr>");
for(n = 0; n < 4; n++)
{
document.write("<tr><td>");
document.write( quest[i].childNodes[n].nodeValue );
document.write("</td></tr>");
}
}
document.write("</table>");
Expected result:
Display each question along with its four answers below it. Currently, only the questions are being displayed correctly.
The format of the XML file is as follows:
<main>
<instruction></instruction>
<solution></solution>
<solution></solution>
<solution></solution>
<solution></solution>
</main>