Моя Javascript функция, кажется, не заканчивается, возможно, из-за бесконечного цикла - PullRequest
0 голосов
/ 17 апреля 2020

Ниже я включил свой код HTML, в нем нет моих исходных терминов и списка определений, исходные списки содержали 859 терминов и 859 определений. Каждый раз, когда я запускаю код, он вылетает в моем локальном браузере. Я не знаю почему. Я думаю, что проблема связана с тестовой функцией, но я точно не знаю.

 <body>
     <div id="test">
     </div>
     <div id="question">
     </div>
     <div>
         A
         <div id="option1">
         </div>
     </div>
     <div>
         B
         <div id="option2">
         </div>
     </div>
     <div>
         C
         <div id="option3">
         </div>
     </div>
     <div>>
         D
         <div id="option4">
         </div>
     </div>
     <button onclick="OptionA()">A</button>
     <button onclick="OptionB()">B</button>
     <button onclick="OptionC()">C</button>
     <button onclick="OptionD()">D</button>

     <script>
         var terms = ["removed all words", "a", "b", "c", "d", "e"]
         var definitions = ["removed all definitions", "a", "b", "c", "d", "e"]
         var testlength = 1;
         var allquestions = [];
         allquestions = terms;
         var questions = [];
         var options = [];
         options = definitions;
         var rightoptions = [];
         var correctanswernumber = 0 - 1;
         questions = terms;
         var qbutton = 0;


         function test() {
             var testlength = 1;
             var allquestions = [];
             allquestions = terms;
             var questions = [];
             var options = [];
             options = definitions;
             var rightoptions = [];
             var correctanswernumber = 0 - 1;
             questions = terms;
             var qbutton = 0;
             qbutton = 1;
             optionlength = options.length;
             testlength = questions.length;
             questionnumber = 22;
             var questionnumber = Math.floor(Math.random() * testlength);
             realquestion = questions[questionnumber];
             document.getElementById("question").innerHTML = realquestion;
             while (allquestions[correctanswernumber] != questions[questionnumber]) {
                 correctanswernumber += 1;
             }
             var correctanswer = options[correctanswernumber];
             var wronganswer1number = Math.floor(Math.random() * optionlength);
             while (wronganswer1number == correctanswernumber) {
                 var wronganswer1number = Math.floor(Math.random() * optionlength);
             }
             var wronganswer2number = Math.floor(Math.random() * optionlength);
             while (wronganswer2number == correctanswernumber) {
                 while (wronganswer2number == wronganswer1number) {
                     var wronganswer2number = Math.floor(Math.random() * optionlength);
                 }
                 var wronganswer2number = Math.floor(Math.random() * optionlength);
             }
             var wronganswer3number = Math.floor(Math.random() * optionlength);
             while (wronganswer3number == correctanswernumber) {
                 while (wronganswer3number == wronganswer1number) {
                     while (wronganswer3number == wronganswer2number) {
                         var wronganswer2number = Math.floor(Math.random() * optionlength);
                     }
                     var wronganswer2number = Math.floor(Math.random() * optionlength);
                 }
                 var wronganswer2number = Math.floor(Math.random() * optionlength);
             }

             wronganswer1 = options[wronganswer1number];
             wronganswer2 = options[wronganswer2number];
             wronganswer3 = options[wronganswer3number];
             var answerchoices = [];
             answerchoices.push(correctanswer, wronganswer1, wronganswer2, wronganswer3);
             var realanswerchoices = [];
             a = Math.floor(Math.random() * 4);
             realanswerchoices.push(answerchoices[a]);
             answerchoices.splice(a, 1, );
             b = Math.floor(Math.random() * 3);
             realanswerchoices.push(answerchoices[b]);
             answerchoices.splice(b, 1, );
             c = Math.floor(Math.random() * 2);
             realanswerchoices.push(answerchoices[c]);
             answerchoices.splice(c, 1, );
             realanswerchoices.push(answerchoices[0]);

             document.getElementById("option1").innerHTML = realanswerchoices[0];
             document.getElementById("option2").innerHTML = realanswerchoices[1];
             document.getElementById("option3").innerHTML = realanswerchoices[2];
             document.getElementById("option4").innerHTML = realanswerchoices[3];
         }
         test();

         function OptionA() {
             document.getElementById("test").innerHTML = "Wrong";
             if (correctanswer == realanswerchoices[3]) {
                 document.getElementById("test").innerHTML = "Right";

             }
             qbutton = 0;

         }

         function OptionB() {
             document.getElementById("test").innerHTML = "Wrong";
             if (correctanswer == realanswerchoices[2]) {
                 document.getElementById("test").innerHTML = "Right";
             }
             qbutton = 0;

         }

         function OptionC() {
             document.getElementById("test").innerHTML = "Wrong";
             if (correctanswer == realanswerchoices[1]) {
                 document.getElementById("test").innerHTML = "Right";
             }
             qbutton = 0;

         }

         function OptionD() {
             document.getElementById("test").innerHTML = "Wrong";
             if (correctanswer == realanswerchoices[0]) {
                 document.getElementById("test").innerHTML = "Right";
             }


         }
     </script>



 </body>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...