简体   繁体   English

为测验添加多个答案

[英]Adding multiple answers to quiz

I need to add multiple questionnaires to use the same code.我需要添加多个问卷才能使用相同的代码。 How can I do this?我怎样才能做到这一点?

// Insert answers to questions
answers[0] = ["Apple", "Test"] ;
answers[1] = "Dynamic HTML";
answers[2] = "Netscape";
answers[3] = "Common Gateway Interface";

As you can see Ive just tried adding my other answer onto the code for a different quiz but it does not recognise.正如您所看到的,我刚刚尝试将我的其他答案添加到不同测验的代码中,但它无法识别。 What is the best way of doing this?这样做的最佳方法是什么? (is there an "or" command or something like that? (是否有“或”命令或类似的东西?

Here is my whole code:这是我的整个代码:

// Insert number of questions
var numQues = 4;

// Insert number of choices in each question
var numChoi = 3;

// Insert number of questions displayed in answer area
var answers = new Array(4);


// Insert answers to questions
answers[0] = ["Apple", "Test"] ;
answers[1] = "Dynamic HTML", "Test";
answers[2] = "Netscape",  "Test";
answers[3] = "Common Gateway Interface", "Test";

// Do not change anything below here ...
function getScore(form) {
var score = 0;
var currElt;
var currSelection;
for (i=0; i<numQues; i++) {
currElt = i*numChoi;
for (j=0; j<numChoi; j++) {
  currSelection = form.elements[currElt + j];
  if (currSelection.checked) {
    if (currSelection.value == answers[i]) {
      score++;
      break;
    }
  }
 }
}
score = Math.round(score/numQues*100);
form.percentage.value = score + "%";
var correctAnswers = "";
for (i=1; i<=numQues; i++) {
correctAnswers += i + ". " + answers[i-1] + "\r\n";
}
form.solutions.value = correctAnswers;
} 
//  End -->

You can use javascript Object literal notation您可以使用 javascript Object literal表示法

var answers = {
    0: ["Apple", "Test"],
    1: "Dynamic HTML",
    2: "Netscape",
    3: "Common Gateway Interface"
};

You can acess your answers with answers[0], answers[1],...您可以使用答案[0]、答案[1]、...访问您的答案。

Do you mean this ?你是这个意思吗?

You need a multidimensional array concept to give more flexible option to answers as follow :您需要一个多维数组概念来为答案提供更灵活的选项,如下所示:

var answers = [
    ['Apple','Test'],
    ['Dynamic HTML','DHTML']
    ['Netscape',''], //you can live this blank
    ['Common Gateway Interface','CGI']
];

//how to access answers like you wanted it more values to compare //如何访问答案,就像你想要更多的值来比较

if((answers [0][0] == 'Appel')||(answers[0][1] == 'Test')) {
   alert('Correct Answer');
}

option two: For more Please refer this question and answer and let me know.选项二:更多请参考这个问题和答案,让我知道。

Tested Here See result在这里测试 查看结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM