全局变量显示为未定义

时间:2017-11-04 19:58:24

标签: javascript jquery

我在这个问题上遇到了其他帖子,但我找不到纠正我的问题的解决方案。这与提升问题或其他我完全遗漏的问题有关吗?

变量' correctanswers'在控制台中显示为未定义。

**编辑我可以确认我已经确保考虑区分大小写。请参阅下文。

enter image description here

$(document).ready(function(){

        var azeem = [

            {
                question: "What is Azeem's favourte color?",
                choices: ["Blue", "Yellow", "Red", "Green"],
                answer: 0
            },

            {
                question: "What is Azeem's favourte movie?",
                choices: ["Scarface", "The Terminator", "Shawshank Redemption", "The Dark Knight"],
                answer: 3
            },

            {
                question: "What was Azeem's first ever job role?",
                choices: ["Cleaner", "Store Assistant", "Sales", "Admin"],
                answer: 1
            },
            {
                question: "What is Azeem's favourite dish?",
                choices: ["Pasta", "Pizza", "Chips", "Curry"],
                answer: 0
            },
            {
                question: "What subject did Azeem enjoy the most in school?",
                choices: ["Drama", "Science", "P.E", "History"],
                answer: 0
            },
            {
                question: "What subject did Azeem least enjoy in school?",
                choices: ["Geography", "Maths", "History", "I.T"],
                answer: 1
            },
            {
                question: "Which one of these cities has Azeem travelled to?",
                choices: ["Madrid", "Lisbon", "Istanbul", "Dublin"],
                answer: 1
            },
            {
                question: "Which college did Azeem study in?",
                choices: ["NewVic", "Redbridge", "East Ham", "Barking"],
                answer: 3
            },
            {
                question: "Who is Azeem's favourite sports icon?",
                choices: ["Eric Cantona", "Muhammad Ali", "Cristiano Ronaldo", "Prince Naseem"],
                answer: 1
            },
            {
                question: "Who is Azeem's favourite music artist?",
                choices: ["Michael Jackson", "Eminem", "Drake", "Linkin Park"],
                answer: 1
            },

        ];

        // counters

        var currentQuestion = 0;
        var questionNumberCounter = 1;
        var correctanswers = 0;

        //elements

        var questionNumber = document.getElementById("questionCount");
        var choices = document.getElementById("choicesSection");
        var questions = document.getElementById("ques");
        var radioButtons = document.getElementsByName("answer");


        // The following event listener will transition from the instructions to the first question of the quiz

        document.getElementById("startquiz").addEventListener("click",function(){
            $(".quiz-intro").fadeOut(600);
            $(".quiz-section").delay(600).slideDown("slow");
            questions.innerText = azeem[currentQuestion].question;
            questionNumber.innerText = questionNumberCounter;
            azeem[currentQuestion].choices.forEach(function(value, index){
                var radio = document.createElement("input");
                var label = document.createElement("label");
                var div = document.createElement("div");
                $(div).addClass("choice");
                radio.setAttribute("type", "radio");
                radio.setAttribute("name", "answer");
                radio.setAttribute("value", index);
                label.innerHTML = value +"<br>";
                choices.appendChild(div);
                div.appendChild(radio);
                div.appendChild(label);

            })

        })


        document.getElementById("submitanswer").addEventListener("click",function(){
            var radioValue = $("input[name='answer']:checked").val();
            if (radioValue === azeem[currentQuestion].answer){
                correctanswers++;
            }
            if (!$('input[name=answer]:checked').length > 0){
                alert("Please select an aswer");
                return; 
            } 
            var labels = document.getElementsByTagName("label");
            questionNumberCounter++;
            questionNumber.innerText = questionNumberCounter;
            currentQuestion++
            if (currentQuestion > 9){
                $(choices).css("display","none");
                document.getElementById("score").innerText = correctanswers;
                $("#quizFinished").css("display","initial");
            }
            questions.innerText = azeem[currentQuestion].question;
            azeem[currentQuestion].choices.forEach(function(value, ind) {
             labels[ind].innerText = value;
                });

            for (var i = 0; i < radioButtons.length; i++){
                    if (radioButtons[i].checked === true){
                        radioButtons[i].checked = false;
                    }
                }

            })





        })

2 个答案:

答案 0 :(得分:1)

您尝试访问不存在的范围内的变量。

correctanswers在您用自己的范围定义:

$(document).ready(function(){})

因此,它未在全局范围内定义,您无法从控制台访问它。

答案 1 :(得分:0)

javascript区分大小写。所以 correctanswers correctAnswers 是不同的