javascript - 继续进行 JavaScript 测验中的下一个元素,因此只要完成一个元素,它就会继续进行下一个元素

标签 javascript

每当您回答完一个问题时,就会以下面的代码结束程序。无论如何,有没有办法做到这样,每当你完成一个问题(可能是第 2 题)时,它就会继续到第 3 题,当它转到最后一个数字时,它就会完成。提前致谢。我今年 11 岁,大约 9 个月前开始编程

                             <!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": ["When was the first apple computer made?],
    "choices": ["1904","1976","1978","2004"],
    "correct": ["1976"]
  }, {
    "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": "Other"
  }]
  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) { // 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()
    }

  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()
  //x+1

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

        }
      }

    }

        </script>


      </body>
    </html>

最佳答案

您似乎已经问过这个问题here 。另一方面,您的代码有问题。

49.60 Unclosed string.

"question": ["When was the first apple computer made?],

我已经为你编写了代码。我使用了一个简单的 JavaScript for 循环,下面是更新后的 sc() 函数:

function sc(){
  for (x = 1; x < 6; x++){ //this is the vital part
    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!")
      clickCounter()
    }
    else if(y=="Cancel"){
      alert("canceled")
      return; //This closes the box
    }
    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);
      }
    }
  } //This is also vital
}

您可以在此处阅读有关 for 循环的更多信息:https://www.tutorialspoint.com/javascript/javascript_for_loop.htm

这是代码示例:http://codepen.io/JamesDouglas/pen/ybzByQ

附注无法关闭该框非常烦人,因此我添加了关闭它的代码。通知用户输入“取消”将关闭该框也很有帮助,因为我在查看代码后才发现。

关于javascript - 继续进行 JavaScript 测验中的下一个元素,因此只要完成一个元素,它就会继续进行下一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44087924/

相关文章:

javascript - 如何推断变量使它们成为全局变量?

javascript - 将自定义 CSS 样式添加到 Material UI KeyboardDatePicker

javascript - jPanelMenu 和滚动不能一起工作

javascript - ng-if 中的 AngularJS 指令不会运行

javascript - 未捕获( promise ): TypeError: Cannot read property 'apply' of undefined in ionic 2 app

javascript - 当我尝试在 native webview 中加载 https 网页时,它显示空白页面

javascript - 当用户选择它并在悬停时更改背景颜色时,JQuery 突出显示带有图像的菜单项

javascript - 在集合矩形中拟合随机多边形的算法

javascript - 在单个构建文件中使用带有 traceur 的 ES6 模块

javascript - 如何在 Photoshop 中访问图层样式信息?