带有差异问题的jQuery测验

时间:2016-01-04 10:20:55

标签: jquery arrays json

我有一个测验来设置10个问题,每个问题有4个答案,其中一个是正确的。好的,这些问题的结构如下:

  • 3个简单的
  • 一个差异问题,有两个答案(基本上下一步取决于这一个。如果用户选择其中一个答案进入路径,显然,如果它选择另一个,他们采取另一个路径
  • 上一个问题正在定义的另外5个问题
  • 一个额外的问题。

现在, 3个简单的 将超过3个,但只有3个将从数组中随机挑选。 差异 只有一个,并且始终保持不变。 另外5个问题 正被分成两个不同的数组(每个特定数组中有超过5个,问题是随机选择的)。一旦用户回答了5个问题,它将根据他或她选择的差异问题从两个不同的阵列中随机选择 一个问题

这应该是脚本背后的逻辑。现在,我的问题是我在这些阵列中没有真正有效率或jSON那么重要而且我对于 if '到底应该怎样才能得到一定的线索?看起来像。

如果有人能指出我正确的方向,我们将非常感激。

1 个答案:

答案 0 :(得分:0)

“在这些数组或JSON中效率不高”意味着您远离自己构建“测验引擎”。尝试制作更小的步骤,并首先了解有关javascript的更多信息。

您可以解析json文件并将其作为javascript数组使用(注意调用函数 onQuestionsLoaded

$.getJSON('https://domain.com/file.json', function(data) {         
   onQuestionsLoaded(data);
});

为了您的目的,我会像这样建立json格式的quizz结构(注意“假设”

[
   {
      "question": "How are you?",
      "order": 1,
      "answers": [
         {
            "option": "a",
            "answer": "Not very well."
         }, {
            "option": "b",
            "answer": "I am pretty fine, thank you."
         }
      ]
   }, {
      "question": "How can I help you then?",
      "order": 2,
      "assume": {"order": 1, "answer": "a"},
      "answers": [
         { .. }
      ]
   }, {
      "question": "Nice to hear that!",
      "order": 2,
      "assume": {"order": 1, "answer": "b"},
      "answers": [
         { .. }
      ]
   }
]

在javascript中你可以使用像

这样的东西
var questions = []; //will hold all questions
var answers = []; //will hold answers given by the user, so we can show questions based on previous answers

function onQuestionsLoaded(data){ //called after json is loaded and parsed
   prepareQuestions(data); //here we prepare array of questions so we can read questions by their order
   showQuestion(1); //showing first question
}

function prepareQuestions(data){
   for(var i = 0; i < data.length; i++)
      if(questions.hasOwnProperty(i))
         questions[data[i].order].push(data[i]);
}

function showQuestion(order){
   var nextQuestion = getNextQuestion(order);
   if(nextQuestion != null){
      $('#currentQuestion').html(nextQuestion.question);
      //and more to show particular question
   }
}

function getNextQuestion(order){
   if(!questions.hasOwnProperty(order)){
      alert('Question with order ['+ order +'] does not exist.');
      return null;
   } else {
      if(questions[order].length > 1){ //if question by given order has more variants
         return pickVariant(order);
      } else return questions[order][0]; //getting a first question in given order
   }   
}

function pickVariant(order){
   for(var i = 0; i < questions[order].length; i++)
      if(questions.hasOwnProperty(i)){
         var question = questions[order][i];
         if(answers[question.assume.order] == question.assume.answer) //desired "if" for checking if the some of previous question was answered by assumed answer
            return question;
      }

   return null; //for case that none of questions belongs to given answer
}

function answerQuestion(order, option){ //you call this when user selects the answer, where the argument "option" is "b" for "I am pretty fine, thank you"
   answers[order] = option; //remember the answer, so we get 1 = b, 2 = a, 3 = d .. and so on
   showQuestion(order + 1); //showing the next question
}

我没有测试过这个。这只是方式,你仍需要在其他地方学习一些关于数组和JSON的知识。