samedi 23 novembre 2019

generating correct answers in mc questions

Can i please have suggestion on how to generate correct answers for each mc questions? i cannot figure out the correct code for giving the correct answers.

Also i want to ask how to randomly assign answers to each choices and keeping one of them as the correct answers.

MC example:

Which of the following words has similar meaning to apple?
A.orange
B.pen
C.rose
D.dance

Thanks in advance.

var ques=['apple', 'pencil' ,'juice','flower','sing','beautiful'];
var ans= ['orange', 'pen','water','rose','dance','pretty'];
var QuesResp = ques.map(function(val, index){
  return [val, ans[index]]
});

var QRpointers = [];

function randOrd() { return (Math.round(Math.random())-0.5); } 

window.onload = function() 
{
  for (var i=0; i<QuesResp.length; i++) { QRpointers.push(i); }
  QRpointers.sort(randOrd);                                    

  var bdy = '<h1>MC questions</h1>';
      Q=0;
  for (var Q=0; Q<1; Q++) 
  {
    bdy += '<div class="qno">Question #'+(Q+1)+'</div> ';
    bdy += '<br/><div class="qu" id="R'+Q+'" > Which of the following words has similar meaning to '+QuesResp[QRpointers[Q]][0]+'?</div><br/>';
    bdy += ' <button class="ansbtn" id="choiceA"   onclick="scoreme()"></button>';
    bdy += ' <button class="ansbtn" id="choiceB"   onclick="scoreme()"></button>';
    bdy += ' <button class="ansbtn" id="choiceC"   onclick="scoreme()"></button>';
    bdy += ' <button class="ansbtn" id="choiceD"   onclick="scoreme()"></button><br/>';
  }
    
    bdy += '<INPUT value="next" type="button" onclick="window.onload()">';
    bdy += '<button onclick="scoreme()">Check All</button>';
    bdy += '<INPUT id="score" type="text" size="8">';
    document.getElementById('QuestionsResponses').innerHTML = bdy;
   
   var anst=Math.floor(Math.random() *ans.length);
   document.getElementById('choiceA').innerHTML= ans[anst];
}
<div id="QuestionsResponses"></div>



Aucun commentaire:

Enregistrer un commentaire