我创建了一个测试,修改了我在网上找到的代码,但我有一个小问题
I have created a quiz, ammending code I have found on the web, but I am having a slight issue
我修改了这段代码,创建了一个测试,用户可以通过检查单选按钮来回答问题。
var totalScore = 0;
var numAnswer = 0;
//an array with objects that hold the questions
var allQuestions = [{question: ["Q1: Who was the Top Goalscorer in the 1998 World Cup?"],
choices: ["Ronaldo", "Davor Sukor", "Zinedine Zindane", "Thierry Henry"],
correctAnswer:1},
{question: ["Q2: Where was the 1998 World Cup Final Held?"],
choices: ["Parc des Princes", "Stade Velodrome", "Stade de France", "Stade de Gerland"],
correctAnswer:2},
{question: ["Q3: What was the score in the 1998 World Cup Final?"],
choices: ["2-0", "2-1", "3-0", "3-1"],
correctAnswer:2},
{question: ["Q4: Who won the 2002 World cup?"],
choices: ["France", "Holland", "Brazil", "Germany"],
correctAnswer:2},
{question: ["Q5: Who was the Top Goalscorer in the 2002 World Cup?"],
choices: ["Vieri", "Ronaldo", "Klose", "Ballack"],
correctAnswer:1}];
//create the questions created by the array, first unselecting all radiobuttons
function createQuestions() {
for (var i = 0; i < 4; i++) {
document.getElementById("answerswer" + i).checked = false;
}
var quizQuestion = document.getElementById("questions");
quizQuestion.innerHTML = allQuestions[this.numAnswer].question[0];
createOptions();
}
//change the innerHTML of the label by calling it's ID
function createOptions() {
for (var o = 0; o <= allQuestions[0].choices[o].length; o++) {
document.getElementById("a" + o).innerHTML = allQuestions[this.numAnswer].choices[o];
}
}
//create an HTMLcollection using document.forms and check selected radiobutton and
//corresponding value.
function checkAnswers() {
var methods = document.forms.radioAnswers.elements.choice;
if (this.numAnswer < 4) {
for (var i = 0; i < methods.length; i++) {
if (methods[i].checked) {
answer = methods[i].value;
}
}
//check if answer is correct answer.
if (parseInt(answer) === allQuestions[this.numAnswer].correctAnswer) {
this.totalScore += 1;
}
this.numAnswer += 1;
createQuestions();
} else {
showScore();
}
}
function showScore() {
var printTotalscore = document.getElementById("questions");
printTotalscore.innerHTML = "Your total score is: " + this.totalScore;
var removeRadio = document.getElementById("questions_form").style.display="none";
var removeButton = document.getElementById("next").style.display="none";
}
window.onload = createQuestions();
我已经阅读了代码,我知道它使用了一系列for循环来创建问题,为问题答案创建HTML,并最终跟踪问题的数量。
关于最后一个问题,分数没有像其他问题一样正确更新,我认为这与循环的期末考试有关,但我已经尽可能多地调试了它,但我仍然不知道如何修复它。
这一行if (this.numAnswer < 4)
是我认为问题所在的地方,因为在最后一个循环之后,它将直接进入函数showScore()
,我可能错了,但如果我将循环更改为(this.numAnswer <= 4)
,则需要再次选择最后一个问题,然后分数是正确的。
有人能帮我解决这个问题吗?
下面是代码笔,也许可以让它变得更容易:http://codepen.io/Addiosamigo/pen/fFHly
感谢
问题似乎是您正确识别的if语句。代码的行为是这样的,因为在最后一个答案上,你没有检查它的漏洞/更新分数。
您需要更改代码,以确保即使在最后一个问题上也能进行分数检查。由于需要始终进行分数检查,最简单的解决方案可能是将if语句移动到只包含显示结果的下一个问题的逻辑。
for (var i = 0; i < methods.length; i++) {
if (methods[i].checked) {
answer = methods[i].value;
}
}
//check if answer is correct answer.
if (parseInt(answer) === allQuestions[this.numAnswer].correctAnswer) {
this.totalScore += 1;
}
// Move the if statement to here
if (this.numAnswer < 4) {
this.numAnswer += 1;
createQuestions();
} else {
showScore();
}
这一点可以在-http://codepen.io/anon/pen/ezgtD.该代码还删除了一些幻数。
相关文章:
- 我有一个严重的问题与CRM的触发器,正在开发与GAS
- 我在角度指令和角度路由中有一个问题
- 我有两个问题!一个是打印,另一个是缩放
- 我有一个问题,使用 Javascript 或 Jquery 创建具有字符串计数的动态字段
- JS说我有一个未定义的对象.真的需要一些帮助来解决这个问题吗
- 我对用php代码创建的HTML有一个问题
- jQuery插件对象:通过.on()附加了一个事件处理程序,现在有一个范围问题.(主插件对象)
- 我创建了一个测试,修改了我在网上找到的代码,但我有一个小问题
- 变量未正确赋值,可能存在作用域问题(getJSON()中的each()中有一个开关的函数)
- 有问题获得一个python cgi脚本打开文件从AJAX传递给它
- 我有一个引导模式的问题-一半的页面加载
- 使用xmlhttp有问题.打开一个dreamweaver模板
- 是否有一个已知的问题,全日历在Safari上以中文呈现(尽管有语言设置?)
- 设计问题:如何设计一个网页,应该显示11个元素,每个元素都有一个描述
- Ajax调用成功,但有一个未定义的问题
- 我有一个问题,当我使用Ext.define
- 我有一个问题与javascript警报功能
- 我认为我'm有一个自定义排序方法的范围问题
- 有一个问题与javascript Jquery和php按钮的复选框
- 我有一个问题,显示javascript输出没有任何用户的行动