[英]Uncaught TypeError: Cannot read property 'quest' of undefined
I know that this question may have been asked before but I believe that this awesome community can forgive me for this :) I am developing (Quiz time) app but there's a bug that's is killing me :( When I run this application every and press the lock button it acts as wanted but after a random number of clicks (may be 4 or 2) It stops and gives me this error : 我知道以前可能会问过这个问题,但是我相信这个很棒的社区可以原谅我:)我正在开发(测验时间)应用程序,但是有一个错误正在杀死我:(当我每次运行该应用程序并按锁定按钮,它按需要运行,但是在随机点击(可能是4或2)之后,它停止了并给了我这个错误:
Uncaught TypeError: Cannot read property 'quest' of undefined
at QuestionReset
Here's the code 这是代码
$(document).ready(function () {
var overwallScore = 0;
//prototype
function Question(quest, option1, option2, option3, correct) {
this.quest = quest;
this.option1 = option1;
this.option2 = option2;
this.option3 = option3;
this.correct = correct;
}
//creating questions
var question1 = new Question("1 + 1", 2, 3, 3, 1);
var question2 = new Question("2 + 2", 4, 3, 3, 2);
var question3 = new Question("3 + 3", 6, 3, 3, 3);
var question4 = new Question("4 + 4", 8, 3, 3, 1);
var question5 = new Question("5 + 5", 10, 3, 3, 2);
var question6 = new Question("6 + 6", 12, 3, 3, 3);
//the questions array
var questions = [];
//pushing questions to array
questions.push(question1);
questions.push(question2);
questions.push(question3);
questions.push(question4);
questions.push(question5);
questions.push(question6);
var randomQuestionIndex = Math.floor(Math.random() * 6 + 1);
function QuestionPre() {
randomQuestionIndex = Math.floor(Math.random() * 6 + 1);
document.querySelector("#question").innerHTML = questions[randomQuestionIndex].quest;
document.querySelector(".option-1").innerHTML = questions[randomQuestionIndex].option1;
document.querySelector(".option-2").innerHTML = questions[randomQuestionIndex].option2;
document.querySelector(".option-3").innerHTML = questions[randomQuestionIndex].option3;
}
QuestionPre();
function QuestionReset() {
randomQuestionIndex = Math.floor(Math.random() * 6 + 1);
document.querySelector("#question").innerHTML = questions[randomQuestionIndex].quest;
document.querySelector(".option-1").innerHTML = questions[randomQuestionIndex].option1;
document.querySelector(".option-2").innerHTML = questions[randomQuestionIndex].option2;
document.querySelector(".option-3").innerHTML = questions[randomQuestionIndex].option3;
}
document.querySelector(".lock-btn").addEventListener("click", function () {
if (document.querySelector(".option-" + questions[randomQuestionIndex].correct + "-check").checked === true) {
alert("Correct, such a Genius !");
overwallScore = overwallScore + 1;
QuestionReset();
} else {
alert("Wrong mate, such a bad luck !");
randomQuestionIndex = Math.floor(Math.random() * 6 + 1);
QuestionReset();
}
}); });
Thanks in advance. 提前致谢。
The problem lies in the line 问题出在线上
var randomQuestionIndex = Math.floor(Math.random() * 6 + 1); var randomQuestionIndex = Math.floor(Math.random()* 6 +1);
The value of randomQuestionIndex
might go out of range. randomQuestionIndex
的值可能超出范围。 Just remove the +1
part and it should work. 只需删除+1
部分,它就可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.