0

How to read the csv in js from like these and process the right answer to grade note: the answers are included in the xml and are seperated by commas

<finalquiz>
<question>  
<qnumber>1</qnumber>  
<qtitle>what is 2+2 </qtitle>  
<a>4</a>  
<b>6</b>  
<c>2</c>  
<d>0</d>  
</question>  
<question>  
<qnumber>2</qnumber>  
<qtitle>what is 3+3 </qtitle>  
<a>4</a>  
<b>6</b>  
<c>9</c>  
<d>3</d>  
<rightanswers>a,b</rightanswers>  
</finalquiz>  
Amitoj
  • 23
  • 4
  • 1
    That is a weird CSV file. So read the text from the xml tag and split it on the comma https://stackoverflow.com/questions/5269856/how-to-split-comma-separated-string-using-javascript – epascarello Apr 02 '19 at 13:53
  • But the problem is how to do i read rightanswers into js, i am able to read the question and options and even put radio buttons on it, but not able to process answers – Amitoj Apr 02 '19 at 13:57
  • 2
    Looks like you need an xml-parser – John Apr 02 '19 at 13:59
  • i used it and i am able to read it as string, Thanks !! , should i use split and make a grade function ? Can you explain a bit on that? – Amitoj Apr 02 '19 at 14:09
  • If you can post the string you have as part of your question, it will be easier for us to suggest solutions. – Cat Apr 02 '19 at 14:28
  • var answer=""; answer += xmldoc.getElementsByTagName("rightanswers")[0].childNodes[0].nodeValue; // this is what i have to read right answer the string is : "a,b" – Amitoj Apr 02 '19 at 14:51

1 Answers1

0

As has been discussed in the comments, you have already parsed the XML and have a string containing the values you need, separated by commas. So the hard work is all done. The solution at this point is very easy - just use "split" to separate the string on the commas. Something like this:

var rightAnswersString = "a,b"; // Assume you have come up with something like this by parsing the XML
var rightAnswersArray = rightAnswersString.split (",");
for (let i=0; i<rightAnswersArray.length; i++) {
   console.log (rightAnswersArray[i]); // Replace console.log with whatever you need to do with each value
}
Duncan
  • 507
  • 3
  • 14