[英]JavaScript Quiz getting right and wrong answers not working
I am creating questions in JS, but some of my answers are not showing up because of quotes and trying to escape HTML.我在 JS 中创建问题,但由于引用和试图逃避 HTML,我的一些答案没有出现。 I am also having trouble getting the correct user answer to give the user plus points or deductions.我也无法获得正确的用户答案来给用户加分或扣分。 Right now, the code is reading every answer as incorrect.现在,代码将每个答案都视为不正确。
Quiz.prototype.guess = function (answer) {
if(this.getQuestionIndex().isCorrectAnswer(answer)) {
right.innerHTML = "Correct!";
this.score += 10;
} else if (!this.getQuestionIndex().isCorrectAnswer(answer)) {
right.innerHTML = "Incorrect!";
wrongTimer();
}
this.questionIndex++;
}
If user is right, they get 10 points.如果用户是对的,他们将获得 10 分。 If user chooses wrong answer, deduct 10 seconds from the timer.如果用户选择错误答案,则从计时器中扣除 10 秒。
Within your snippet it turns out that answer
is just the actually clicked text.在您的代码段中,事实证明answer
只是实际点击的文本。
Since the isCorrectAnswer
checks it against the choice
key of your question class which is ony "1"
, "2"
and so on it will never match.由于isCorrectAnswer
根据您的问题 class 的choice
键检查它,只有"1"
, "2"
等等,它永远不会匹配。
Change your question definition like this and it should work:像这样更改您的问题定义,它应该可以工作:
new Question(
"2. What company developed JavaScript?",
["Java Inc.",
"Netscape",
"JQuery",
"CERN"],
"Netscape"
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.