请帮我修改我的代码。在我的测验中,我有2个按钮,其中包含easyQuiz或hardQuiz功能,其中一个可以让您进行简单的测验,其中一个可以帮助您轻松完成。它有什么价值的难度,它应该使用那一系列的答案。我遇到的问题是我不认为正在加载答案数组,因为我收到一条错误,说'答案'没有定义。
// Insert number of questions
var numQues = 4;
// Insert number of choices in each question
var numChoi = 3;
// To set quiz difficulty
var difficulty = 0;
function easyQuiz (difficulty){
difficulty = 1;
// Insert number of questions displayed in answer area
var answers = new Array(4);
// Insert answers to questions
answers[0] = ["Apple"] ;
answers[1] = ["Dynamic HTML"];
answers[2] = ["Netscape"];
answers[3] = ["Common Gateway Interface"];
}
function hardQuiz (difficulty) {
difficulty = 2;
// Insert number of questions displayed in answer area
var answers = new Array(4);
// Insert answers to questions
answers[0] = ["Test"] ;
answers[1] = ["Test"];
answers[2] = ["Test"];
answers[3] = ["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 -->
答案 0 :(得分:2)
您在函数内部声明了答案变量。
如果要访问答案数组,则必须在较高范围内声明它。
var answers = [];
function easyQuiz (){
difficulty = 1;
answers = ["Apple", "Dynamic HTML", "Netscape", "Common Gateway Interface"];
}
答案 1 :(得分:2)
你在函数中声明answer
变量,这意味着它们是函数的本地变量,并且一旦函数结束就不再存在。
在函数外部声明变量以使其成为全局变量。
此外,您将数组作为项目放在数组中,根据代码的其余部分判断,您应该将字符串放在数组中。
var answers;
function easyQuiz (difficulty){
difficulty = 1;
// Insert number of questions displayed in answer area
answers = new Array(4);
// Insert answers to questions
answers[0] = "Apple";
answers[1] = "Dynamic HTML";
answers[2] = "Netscape";
answers[3] = "Common Gateway Interface";
}
function hardQuiz (difficulty) {
difficulty = 2;
// Insert number of questions displayed in answer area
answers = new Array(4);
// Insert answers to questions
answers[0] = "Test";
answers[1] = "Test";
answers[2] = "Test";
answers[3] = "Test";
}