gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'quest' of undefined

转载 作者:行者123 更新时间:2023-11-28 04:07:54 31 4
gpt4 key购买 nike

我知道这个问题之前可能已经被问过,但我相信这个很棒的社区可以原谅我:)我正在开发(测验时间)应用程序,但有一个错误正在杀死我:(当我每次运行此应用程序并按下锁定按钮时,它会按预期运行,但在随机单击次数(可能是 4 或 2)后,它会停止并给出此错误:

         Uncaught TypeError: Cannot read property 'quest' of undefined
at QuestionReset

这是代码

        $(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();


}
}); });

提前致谢。

最佳答案

问题出在线路上

var randomQuestionIndex = Math.floor(Math.random() * 6 + 1);

randomQuestionIndex 的值可能超出范围。只需删除 +1 部分即可。

关于javascript - 未捕获的类型错误 : Cannot read property 'quest' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46529698/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com