Javascript 试图为我的编码测验编写检查答案,但它不是';如果工作不正常,我缺少什么?

Javascript 试图为我的编码测验编写检查答案,但它不是';如果工作不正常,我缺少什么?,javascript,dom,events,Javascript,Dom,Events,以下是我正在努力工作的代码: function checkAnswer(questionIndex) { // This ID goes to a div storing the four buttons var answersId = document.getElementById("answers"); //The four spans inside the buttons which has data-answer, targeting at their id. The id

以下是我正在努力工作的代码:

function checkAnswer(questionIndex) {

// This ID goes to a div storing the four buttons
    var answersId = document.getElementById("answers");

//The four spans inside the buttons which has data-answer, targeting at their id. The id and data-answer are the same value.

    var answer0 = document.getElementById("option0").getAttribute("data-answer");
    var answer1 = document.getElementById("option1").getAttribute("data-answer");
    var answer2 = document.getElementById("option2").getAttribute("data-answer");
    var answer3 = document.getElementById("option3").getAttribute("data-answer");

    answersId.addEventListener("click", function (event) {

        if (event.target.hasAttribute("data-answer")) {

            event.preventDefault;

            if (answer0 == questionList[questionIndex].correctAnswer) {
                console.log(answer0)
                score++;
                console.log(score)
                console.log("This is option0")

            }

            else if (answer1 == questionList[questionIndex].correctAnswer) {

                console.log(answer1)
                score++;
                console.log(score)
                console.log("This is option1")


            } else if (answer2 == questionList[questionIndex].correctAnswer) {

                console.log(answer2)
                score++;
                console.log(score)
                console.log("This is option2")

            } else if (answer3 == questionList[questionIndex].correctAnswer) {

                console.log(answer3)
                score++;
                console.log(score)
                console.log("This is option3")

            } else {

                console.log(score)
            }
        }
    });
}
这是一些硬编码现在,但我想确保它实际工作之前,优化它

基本上,我有多项选择题测验的按钮,其想法是,根据点击的按钮,他们可以得到选择正确答案的分数。在这种情况下,数据答案应该与正确答案匹配。(如果正确答案为选项1,则“选项1”=“选项1”)

但无论单击哪个按钮(在本例中,答案确实是option1),它都会给出点并给出选项1的console.log,这意味着即使在我选择其他按钮时,它也会不断选择if语句

错误的答案,也就是else语句,没有被识别出来,这简直让我抓狂 或者更确切地说,尽管点击了错误的答案,它仍然能够识别第二个if语句。我认为我的逻辑是正确的,只是不正确

你知道我会错过什么吗

编辑:下面是被引用的问题列表数组,以及每个变量的对象:

var question1 = {
    text: "Commonly used data types do NOT include:",
    choices: ["1 - Booleans", "2 - Alerts", "3 - Strings", "4 - Numbers"],
    correctAnswer: "option1",
};

var question2 = {

    text: "The condition of an if/else statement is enclosed within ______.",
    choices: ["1 - Quotes", "2 - Curly Brackets", "3 - Parentheses", "4 - Square Brackets"],
    correctAnswer: "option2",
};

var question3 = {
    text: "Arrays in Javascript can be used to store ______.",
    choices: ["1 - Numbers and strings", "2 - Other Arrays", "3 - Booleans", "4 - All of the above",],
    correctAnswer: "option3",
};

var question4 = {
    text: "String values must be enclosed within ______ when being assigned to variables.",
    choices: ["1 - Quotes", "2 - Curly Brackets", "3 - Commas", "4 - Parentheses"],
    correctAnswer: "option0",
};

var question5 = {
    text: "A very useful tool used during development and debugging for printing content to the debugger is:",
    choices: ["1 - Javascript", "2 - console.log", "3 - Terminal/bash", "4 - For loops"],
    correctAnswer: "option1",
};

var questionList = [question1, question2, question3, question4, question5];
编辑2:

添加调用函数的位置。这次我将eventlistener从checkAnswer()函数中移出,正好在调用它之前

document.getElementById("start-quiz").addEventListener("click", function (event) {

    event.preventDefault;
    event.stopPropagation;

    countDown = 75;
    countDownSpan.textContent = countDown;
    document.querySelector("#description").style.display = "none";
    document.querySelector("#start-quiz").style.display = "none";
    contentId.style.textAlign = "left";
    setTime();
    createAnswers();
    generateQA(0);

    var answersId = document.getElementById("answers");

    answersId.addEventListener("click", function (event) {

        checkAnswer(0)

    });
});
编辑3:

如果有帮助,以下是指向github页面的链接:


GitHub

编辑问题并将您的
questionList
定义放入其中。我已经添加了questionList定义以及每个元素的对象定义。当你说编辑我的问题时,你想让我重新表述我文章的标题问题吗?我在标题末尾添加了一个问题,你正在调用
answersId.addEventListener(“单击“,…)
在你的
checkAnswer
函数中。你的意思是在函数之外做吗?我已经用两种方法来测试它了,但是结果是一样的,只是上次我提交它时,我把它放在了里面。