Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.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,所以我有一段代码,一旦它完成一个问题,它就停止了。不管怎样,一旦你选择了一个问题,完成了这个问题,并且把它弄对了,它就会继续到数组中的下一个问题,但是如果你弄错了,它就会停止 我还有一个小错误,当你输入一个问题编号,然后按enter键,它会转到数组中的问题0 如果你能帮忙,请做,并提前感谢。我11岁,大约一年前开始编程 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <

所以我有一段代码,一旦它完成一个问题,它就停止了。不管怎样,一旦你选择了一个问题,完成了这个问题,并且把它弄对了,它就会继续到数组中的下一个问题,但是如果你弄错了,它就会停止

我还有一个小错误,当你输入一个问题编号,然后按enter键,它会转到数组中的问题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>

你比五年级的学生聪明吗?!!
使琐事再次伟大!
你比五年级的学生还聪明吗?
开始

重设分数 函数starter(){ 设置超时(“clickCounter()”,100) setTimeout(“minusCounter()”,101)} 函数重置(){ 设置超时(“clickCounter()”,100) localStorage.clickcount=-1 } 函数clickCounter(){ if(类型(存储)!=“未定义”){ if(localStorage.clickcount){ localStorage.clickcount=数字(localStorage.clickcount)+1; }否则{ localStorage.clickcount=0; } document.getElementById(“结果”).innerHTML=“分数:”+ localStorage.clickcount }否则{ } } 函数计数器(){ if(类型(存储)!=“未定义”){ if(localStorage.clickcount){ localStorage.clickcount=Number(localStorage.clickcount)-1; }否则{ localStorage.clickcount=0; } document.getElementById(“结果”).innerHTML=“分数:”+ localStorage.clickcount }否则{ } } if(!(“Array.prototype中的扰码”){ Object.defineProperty(Array.prototype,“扰码”{ 可枚举:false, 值:函数(){ var o,i,ln=该长度; 而(ln--){ i=Math.random()*(ln+1)| 0; o=此[ln]; 此[ln]=此[i]; 这个[i]=o; } 归还这个; } }); } 变量测验=[{ “问题”:[“编辑器中的Bug”], “选项”:[“请忽略、输入垃圾邮件或输入取消或其他”], “正确”:[“其他”] }, { “问题”:“谁是微软的创始人?”, “选择”:[“比尔·盖茨”、“史蒂夫·乔布斯”、“史蒂夫·沃兹尼亚克”、“马丁” 沙巴”], “正确”:“比尔·盖茨” }, { “问题”:“你的第一个梦想是什么?”, “选项”:[“8位”、“64位”、“1024位”], “正确”:“8位” }, { “问题”:“C编程语言是由谁开发的?”, “选择”:[“Brendan Eich”,“Dennis Ritchie”,“Guido van Rossum”], “正确”:“丹尼斯·里奇” }, { “问题”:“CC在电子邮件中是什么意思?”, “选择”:[“复写”、“知识共享”、“其他”], “正确”:“复印件” }, { “问题”:“IP的完整用途是什么”, “选项”:[“互联网提供商”、“互联网端口”, “其他”、“互联网协议”], “正确”:“复印件” }] 函数停止(){ 警报(“停止”) } 函数sc(){ quick.forEach(q=>q.choices.scramble()); var x=提示(“选择开始问题编号:”; 如果(x>=6){ 提醒(“请选择一个有效的问题”) sc() } else if(x0){ } else如果(x=0){ 提醒(“请选择一个有效的问题”) sc() } else{alert(“请选择一个有效的问题”),sc()} var ans=“” 函数myFunction(项目、索引){ ans+=“\n[”+(索引+1)+“]:“+项; } 测验[x].choices.forEach(myFunction); 变量y=提示(测验[x]。问题+“\n您的答案是:”+ans); 如果(y==测验[x]。正确){ 警告(“正确!”) 单击计数器() } 如果(y==“取消”){警报(“取消”)} 否则{ 警告(“错误!请重试”); 重复() } 函数重复(){ quick.forEach(q=>q.choices.scramble()); var ans=“” 函数myFunction(项目、索引){ ans+=“\n[”+(索引+1)+“]:“+项; } 测验[x].choices.forEach(myFunction); 变量y=提示(测验[x]。问题+“\n您的答案是:”+ans); 如果(y==测验[x]。正确){ 警惕(“正确,干得好”) 单击计数器() } 如果(y==“取消”){警报(“取消”)} 否则{ 警告(“对不起!\n正确答案是”+测验[x]。正确); } } }
对不起,我不太明白你的问题。 你能多告诉我一些吗

如果您的意思是,当您尝试在number中键入answer选项时,它从未返回true,那么您的值将比较
number
正确的字符串


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

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

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

整理您的代码。您可能认为需要花费大量的时间和精力来完成这项工作,但事实是,您最终节省了大量的调试时间

事实上,专业程序员的大部分时间都花在计划和调试上,而不是编码本身。因此,简化调试非常重要。第一步是编写整洁的代码

你的问题很好地说明了这一点:你应该
        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()
        }