开发者问题收集

我希望它继续在 javascript 测验中转到数组中的下一个元素

2017-05-19
90

所以我有一段代码,一旦完成一个问题它就会停止,有没有什么办法,一旦你选择一个问题并完成这个问题并且答对了它就会继续处理数组中的下一个问题,但是如果你答错了它就会停止。

我还有一个小错误,当你输入一个问题编号并按下回车键时它会继续处理数组中的第 0 个问题

如果你能帮忙请帮忙,提前谢谢了。我 11 岁,大约一年前开始编程。

<!DOCTYPE html>
   <html>
     <head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Are you smarter than a 5th Grader?!!</title>
<link rel="stylesheet" href="style.css">
     </head>
     <body onload="starter()" bgcolor="lightblue">
     <h1><marquee><font color="red">Make Trivia Great Again!</font>       
       </marquee></h1>
     <h2><em><center>Are You Smater Than a 5th Grader?</center></em></h2>
       <button onclick="sc()">Start</button><br>
       <p id="demo"> </p>
       <div id="result"></div>
       <button onclick="reset()">Reset Score</button>
       <script>
         function starter(){ 
           setTimeout("clickCounter()",100)
           setTimeout("minusCounter()",101)}
         function reset(){
           setTimeout("clickCounter()",100) 
           localStorage.clickcount=-1

         }
  function clickCounter() {
    if(typeof(Storage) !== "undefined") {

      if (localStorage.clickcount) {
        localStorage.clickcount = Number(localStorage.clickcount)+1;
      } else {
        localStorage.clickcount = 0;
      }
      document.getElementById("result").innerHTML = "Score:"+                                      
             localStorage.clickcount 
           } else {
                  }   } 
      function minusCounter(){

        if(typeof(Storage) !== "undefined") {

          if (localStorage.clickcount) {
            localStorage.clickcount = Number(localStorage.clickcount)-1;
          } else {
            localStorage.clickcount = 0;
          }
          document.getElementById("result").innerHTML = "Score:"+                               
          localStorage.clickcount 
        } else {
        } 
      }

         if (!("scramble" in Array.prototype)) {
           Object.defineProperty(Array.prototype, "scramble", {
             enumerable: false,
             value: function() {
        var o, i, ln = this.length;
        while (ln--) {
          i = Math.random() * (ln + 1) | 0;
          o = this[ln];
          this[ln] = this[i];
          this[i] = o;
        }
        return this;
             }
           });
         }
         var quiz = [{
           "question": ["Bug in the editor"],
           "choices": ["Please Ignore,Spam Enter or enter Cancel or Other"],
           "correct": ["Other"]
         }, {
                  "question": "Who is the founder of Microsoft?",
           "choices": ["Bill Gates", "Steve Jobs", "Steve Wozniak" , "Martin        
                 Shaba"],
           "correct": "Bill Gates"
         }, {
           "question": "What was your first dream?",
           "choices": ["8 bits", "64 bits", "1024 bits"],
           "correct": "8 bits"
         }, {
           "question": "The C programming language was developed by?",
           "choices": ["Brendan Eich", "Dennis Ritchie", "Guido van        
           Rossum"],
           "correct": "Dennis Ritchie"
         }, {
           "question": "What does CC mean in emails?",
           "choices": ["Carbon Copy", "Creative Commons", "other"],
           "correct": "Carbon Copy"
         }, {
           "question": "What is the full for of IP",
           "choices": ["Internet provider", "Intenet Port",               
          "Other","Internet                             Protocol"],
                  "correct": "Carbon Copy"
         }]
         function stop(){
           alert("stopped")
         }
   function sc(){
         quiz.forEach(q => q.choices.scramble());
         var x = prompt("Select Start question number #:");
               if (x>=6){
    alert("please pick a valid question")
                 sc()
         }
     else if (x<=5&&x>0){

     }

     else if(x=0){
       alert("please pick a valid question")
              sc()
     }

         else{alert("Please pick a valid question"),sc()}

         var ans = ""
         function myFunction(item, index) {
           ans += "\n[" + (index+1) + "]: " + item ; 
         }
         quiz[x].choices.forEach(myFunction);

         var y = prompt(quiz[x].question+"\nYour anwser is:"+ans);

         if (y == quiz[x].correct){
           alert("Correct!")
           clickCounter()

         }
            else if(y=="Cancel"){alert("canceled")}
     else{
           alert("Wrong! Please Try Again.");
           repeat()
         }
     function repeat(){
       quiz.forEach(q => q.choices.scramble());
       var ans = ""
       function myFunction(item, index) {
         ans += "\n[" + (index+1) + "]: " + item ; 
       }
       quiz[x].choices.forEach(myFunction);

       var y = prompt(quiz[x].question+"\nYour anwser is:"+ans);

       if (y == quiz[x].correct){
         alert("Correct!,Good Job")
         clickCounter()


       }
       else if(y=="Cancel"){alert("canceled")}
       else{
         alert("Sorry! \nThe right answer is "+quiz[x].correct);

       }
     }

   }

      </script>


     </body>
   </html>
2个回答

首先,欢迎来到编程世界,年轻的程序员 :)

在回答你的问题之前,我想给你以下建议:

整理你的代码。 你可能认为你需要花费大量的时间和精力来做这件事,但事实是 - 你最终会节省大量的调试时间。

专业程序员实际上大部分时间都花在规划和调试上,而不是编码本身。因此,让调试变得简单很重要。做到这一点的第一步是编写整洁的代码。

你的问题很好地证明了这一点:你不知道发生了什么错误, 你看不到它 。 (对于其他人来说,阅读和帮助也很痛苦)

这实际上是两个简单的问题:

  1. 您的 sc() 函数上有一个无限循环函数调用
    var x 的 else-if 情况下,您需要添加 return 以退出该函数。
  2. 您几乎总是通过错误地输入 x = 0 而不是 x == 0 来触发此无限循环>
    (假设您知道 x = 0x == 0 不同)

因此,以下是所涉及的代码:

        if (x >= 6) {
            alert("please pick a valid question")
            sc()
        } else if (x <= 5 && x > 0) {

        } else if (x == 0) { // x = 0 is assignment, not comparison
            alert("please pick a valid question")
            sc()
            return;  // You have to 'return' here
                     // otherwise the code following the else would continue to execute after this inner-sc() returns
        } else {
            alert("Please pick a valid question"), sc()
        }

此外,您的测验数组的字符串中有换行符。 我不确定这是否只是您的副本 &粘贴到 Stack Overflow 添加了这些换行符,但如果没有,字符串不应包含换行符。如果需要添加换行符,请使用换行符 \n (反斜杠 n)

kazenorin
2017-05-19

抱歉,我没能清楚理解你的问题。 你能告诉我更多吗?

如果你的意思是,当你尝试在数字中输入答案选项时,它永远不会返回 true,那么你比较的是 数字“正确”字符串

你应该尝试比较相同的数据类型以获得所需的结果。

Toan Tran
2017-05-19