javascript - 我想做一个 Javascript 测验,每次都会随机化问题

标签 javascript

我想做一个每次问题随机化的测验。我得到的东西可以随机化答案,但不能随机化问题。这是我到目前为止所得到的。

       <!DOCTYPE html>
       <html>
         <head>
           <meta charset="utf-8">
           <meta name="viewport" content="width=device-width, initial-                                                                             scale=1">
<title>Made with Thimble</title>
<link rel="stylesheet" href="style.css">

<p>

</p>
<script>  
  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": ["What is the full form of IP?","hi"],
    "choices": ["Internet Provider", "Internet Port", "Internet Protocol" , "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"
  }];

  quiz.forEach(q => q.choices.scramble());
  document.write(quiz[prompt("Which one?")].choices);
</script>

最佳答案

您只能提示一次,然后存储在变量中以选择所有测验元素..

quiz.forEach(q => q.choices.scramble());    
var x = prompt("Select question number #:");
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!");
}else{
    alert("Wrong! \nThe right answer is "+quiz[x].correct);
}

关于javascript - 我想做一个 Javascript 测验,每次都会随机化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44015077/

相关文章:

Javascript:在特定数组结构上使用 array.sort()

javascript - 使用 javascript 进行 Google 电子表格搜索

javascript - Internet Explorer 将 e.which 显示为未定义

javascript - 如何将动态创建的对象绑定(bind)到文档就绪事件?

javascript - 根据鼠标指针改变div的位置

javascript - 将另一个数组中的数组插入到 for 循环中,重复第一个结果

javascript - 在 Javascript 中存储多个参数和值

javascript - 我试图理解 Javascript 中的这段嵌套对象

javascript - 如何 .move() 选定的画笔?

javascript - Yammer 开放图 API 错误 400