Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在正确回答后带来正确的信息_Javascript - Fatal编程技术网

Javascript 在正确回答后带来正确的信息

Javascript 在正确回答后带来正确的信息,javascript,Javascript,我试图创建一条消息,在用户从选项中选择正确答案后显示正确,但我不知道为什么我的代码没有这样做。我已经添加了完整的代码。经过这么多的变化,我不知道为什么它仍然没有通过。问题和答案以对象数组的形式出现 <script> var questions = [{"category":"Science: Computers","type":"multiple","difficulty":"easy","question":"What does CPU stand for?",

我试图创建一条消息,在用户从选项中选择正确答案后显示正确,但我不知道为什么我的代码没有这样做。我已经添加了完整的代码。经过这么多的变化,我不知道为什么它仍然没有通过。问题和答案以对象数组的形式出现

 <script>
         var questions = [{"category":"Science: Computers","type":"multiple","difficulty":"easy","question":"What does CPU stand for?","correct_answer":"Central Processing Unit","answers":["Central Process Unit","Computer Personal Unit", "Central Processing Unit", "Central Processor Unit"]},
         {"category":"Science: Computers","type":"multiple","difficulty":"easy","question":"In the programming language Java, which of these keywords would you put on a variable to make sure it doesn&#039;t get modified?","correct_answer":"Final","answers":["Static", "Final", "Private","Public"]},
         {"category":"Science: Computers","type":"boolean","difficulty":"easy","question":"The logo for Snapchat is a Bell.","correct_answer":"False","answers":["True", "False"]},
         {"category":"Science: Computers","type":"boolean","difficulty":"easy","question":"Pointers were not used in the original C programming language; they were added later on in C++.","correct_answer":"False","answers":["True", "False"]},
         {"category":"Science: Computers","type":"multiple","difficulty":"easy","question":"What is the most preferred image format used for logos in the Wikimedia database?","correct_answer":".svg","answers":[".svg", ".png",".jpeg",".gif"]},
         {"category":"Science: Computers","type":"multiple","difficulty":"easy","question":"In web design, what does CSS stand for?","correct_answer":"Cascading Style Sheet","answers":["Counter Strike: Source","Corrective Style Sheet","Computer Style Sheet", "Cascading Style Sheet"]},
         {"category":"Science: Computers","type":"multiple","difficulty":"easy","question":"What is the code name for the mobile operating system Android 7.0?","correct_answer":"Nougat","answers":["Ice Cream Sandwich", "Nougat", "Jelly Bean","Marshmallow"]},
         {"category":"Science: Computers","type":"multiple","difficulty":"easy","question":"On Twitter, what is the character limit for a Tweet?","correct_answer":"140","answers":["120","160","100", "140"]},
         {"category":"Science: Computers","type":"boolean","difficulty":"easy","question":"Linux was first created as an alternative to Windows XP.","correct_answer":"False","answers":["True", "False"]},
         {"category":"Science: Computers","type":"multiple","difficulty":"easy","question":"Which programming language shares its name with an island in Indonesia?","answer":"Java","incorrect_answers":["Python","C","Jakarta", "Java"]}];

         window.onload =function(){




                 var questionIndex = -1; // Not started


                    function submitAnswer() {
                    //document.body.innerHTML = '';
                    ++questionIndex;
                        document.write(questions[questionIndex].question + "<br />");

                            for (var j=0; j < questions[questionIndex].answers.length; j++) {
                            document.write("<input type=radio id=myRadio name=radAnswer>" + questions[questionIndex].answers[j] + "<br />");


                            }



                    if (questionIndex < (questions.length - 1)) {
                        var nextButton = document.createElement("input");
                        nextButton.type = "button";
                        nextButton.value = "Submit Answer";
                        nextButton.addEventListener('click', submitAnswer);
                        document.body.appendChild(nextButton);
                    }

                    var userAnswer,
                    element = document.querySelector("#myRadio:checked");
                    if (element !== null) {
                        userAnswer = element.value;

                        } else {
                            userAnswer = null;
                            return "Select Answer"
                        }

                        if (userAnswer == questions[questionIndex].correct_answers) {
                        var message,
                        element = document.querySelector("#results");
                        if (element !== null) {
                            message = "Correct";
                        } else {
                            message = null;
                            return "Select Answer";
                        }


                    }


                    };

                    submitAnswer();

一切正常,直到这一行:

document.body.appendChild(message);

您试图附加字符串值,但它需要一个节点对象,即参数1不是“节点”类型。因此,您的代码不能正常工作。

您使用索引j,但它没有初始化。。。userAnswer==问题[questionIndex]。正确答案[j]。这是在上面创建的:您好,欢迎来到stackoverflow。我们需要更多的代码来帮助您。questins、questionIndex和j的定义在哪里?我已经更新了我的问题