简体   繁体   中英

Bringing Correct Message after correct answer

I am trying to create a message that shows "Correct" after user select a correct answer from options but I dont know why my code is not doing that. I have added the full code. After so many changes, I dont know why it is still not going through. The question and answer are in array of objects.

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

Everything is fine until this line:

document.body.appendChild(message);

You are trying to append a String value,but it's expecting a Node Object (ie parameter 1 is not of type 'Node'). Hence your code is not working properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM